initial support for streams
This commit is contained in:
1 parent
07244aaf11
commit
77b3754ff4
3 files changed
+118
-1
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import io
|
||||
from pdfmajor.parser_v3.objects.stream import PDFStream
|
||||
from typing import Iterator, Optional
|
||||
|
||||
from pdfmajor.lexer import PDFLexer
|
||||
@@ -85,6 +86,24 @@ class PDFParser:
|
||||
yield last_ctx.clone()
|
||||
elif self.strict:
|
||||
raise ParserError(f"Invalid position for 'endobj' {token}")
|
||||
elif token.value == b"stream":
|
||||
last_ctx = self.state.current_context
|
||||
if last_ctx is not None and isinstance(last_ctx, IndirectObject):
|
||||
stream = PDFStream(token.end_loc, 0)
|
||||
stream.pass_item(last_ctx.get_object())
|
||||
last_ctx.save_object(PDFStream(token.end_loc, 0))
|
||||
self.state.context_stack.append(stream)
|
||||
self.lexer.seek(stream.offset + stream.length)
|
||||
else:
|
||||
raise ParserError(
|
||||
f"PDFStream is missing initilization dictionary"
|
||||
)
|
||||
elif token.value == b"endstream":
|
||||
stream = self.state.context_stack.pop()
|
||||
if not isinstance(stream, PDFStream):
|
||||
raise ParserError(f"Invalid position for 'endstream' {token}")
|
||||
else:
|
||||
raise ParserError(f"Invalud Keyword {token}")
|
||||
elif isinstance(token, TokenArray):
|
||||
for obj in self.state.flush_int_collections():
|
||||
yield obj
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
from pdfmajor.parser_v3.exceptions import ParserError
|
||||
from typing import Any, List, Optional
|
||||
from .base import PDFContextualObject, PDFObject
|
||||
from .primitives import PDFName
|
||||
from .collections import PDFDictionary
|
||||
|
||||
|
||||
class PDFStream(PDFContextualObject):
|
||||
"""A class representing a PDF Stream as specified in PDF spec 1.7 section 7.3.8"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
offset: int = 0,
|
||||
length: int = 0,
|
||||
filter: Optional[List[PDFName]] = None,
|
||||
decode_parms: Optional[List[PDFDictionary]] = None,
|
||||
f: Optional[Any] = None, # file-specifications
|
||||
ffilter: Optional[List[PDFName]] = None,
|
||||
fdecode_parms: Optional[List[PDFDictionary]] = None,
|
||||
dl: Optional[int] = None,
|
||||
) -> None:
|
||||
"""
|
||||
Args:
|
||||
offset (int): the offset where the stream itself starts
|
||||
length (int): the length of the stream
|
||||
filter (Optional[List[PDFName]], optional): The names of a filter that shall be applied in processing the stream data. Defaults to None.
|
||||
decode_parms (Optional[List[PDFDictionary]], optional): Parameter dictionaries used by the filters specified by filter. Defaults to None.
|
||||
f (Optional[Any], optional): The file containing the stream data. If this entry is present, the bytes between stream and endstream shall be ignored. However, the Length entry should still specify the number of those bytes (usually, there are no bytes and Length is 0).. Defaults to None.
|
||||
ffilter (Optional[List[PDFName]], optional): The names of a filter that shall be applied in processing of the external file. Defaults to None.
|
||||
fdecode_parms (Optional[List[PDFDictionary]], optional): Parameter dictionaries used by the filters specified by ffilter. Defaults to None.
|
||||
dl (Optional[int], optional): A non-negative integer representing the number of bytes in the decoded (defiltered) stream. It can be used to determine, for example, whether enough disk space is available to write a stream to a file.This value shall be considered a hint only; for some stream filters, it may not be possible to determine this value precisely. Defaults to None.
|
||||
"""
|
||||
self.offset = offset
|
||||
self.length = length
|
||||
self.filter = filter
|
||||
self.decode_parms = decode_parms
|
||||
self.f = f
|
||||
self.ffilter = ffilter
|
||||
self.fdecode_parms = fdecode_parms
|
||||
self.dl = dl
|
||||
|
||||
def pass_item(self, item: PDFObject) -> None:
|
||||
if not isinstance(item, PDFDictionary):
|
||||
raise ParserError(f"Cannot initilize a pdfstream with {type(item)}")
|
||||
data = item.to_python()
|
||||
self.length = data["Length"]
|
||||
self.filter = data.get("Filter", self.filter)
|
||||
self.decode_parms = data.get("DecodeParms", self.decode_parms)
|
||||
self.f = data.get("F", self.f)
|
||||
self.ffilter = data.get("FFilter", self.ffilter)
|
||||
self.fdecode_parms = data.get("FDecodeParms", self.fdecode_parms)
|
||||
|
||||
def to_python(self):
|
||||
return {
|
||||
"offset": self.offset,
|
||||
"length": self.length,
|
||||
"filter": self.filter,
|
||||
"decode_parms": None
|
||||
if self.decode_parms is None
|
||||
else [x.to_python() for x in self.decode_parms],
|
||||
"f": self.f,
|
||||
"ffilter": self.ffilter,
|
||||
"fdecode_parms": None
|
||||
if self.fdecode_parms is None
|
||||
else [x.to_python() for x in self.fdecode_parms],
|
||||
"dl": self.dl,
|
||||
}
|
||||
+32
-1
@@ -7,8 +7,9 @@ from unittest import TestCase
|
||||
from pdfmajor.streambuffer import StreamEOF
|
||||
from pdfmajor.parser_v3.objects.indirect import IndirectObject, IndirectObjectCollection
|
||||
from pdfmajor.parser_v3.objects.base import PDFObject
|
||||
from pdfmajor.parser_v3 import PDFParser
|
||||
from pdfmajor.parser_v3.objects.comment import PDFComment
|
||||
from pdfmajor.parser_v3.objects.stream import PDFStream
|
||||
from pdfmajor.parser_v3 import PDFParser
|
||||
|
||||
|
||||
class Collections(TestCase):
|
||||
@@ -177,3 +178,33 @@ class IndirectObjects(TestCase):
|
||||
indobjc.get_indobject(11, 1),
|
||||
],
|
||||
)
|
||||
|
||||
def test_parse_stream(self):
|
||||
parser = PDFParser(
|
||||
io.BytesIO(
|
||||
br"""
|
||||
5 0 obj
|
||||
<< /Type /XRef /Length 124 /Filter /FlateDecode >>
|
||||
stream
|
||||
"<99><A3><C1>l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567890l01234567
|
||||
<80>endstream
|
||||
endobj
|
||||
"""
|
||||
)
|
||||
)
|
||||
for obj in parser.iter_objects():
|
||||
self.assertIsInstance(obj, IndirectObject)
|
||||
self.assertIsInstance(obj.get_object(), PDFStream)
|
||||
self.assertDictEqual(
|
||||
obj.get_object().to_python(),
|
||||
{
|
||||
"offset": 78,
|
||||
"length": 0,
|
||||
"filter": None,
|
||||
"decode_parms": None,
|
||||
"f": None,
|
||||
"ffilter": None,
|
||||
"fdecode_parms": None,
|
||||
"dl": None,
|
||||
},
|
||||
)
|
||||
Reference in new issue
Block a user