pages nodes
This commit is contained in:
1 parent
e33d13a3e1
commit
da6e7a7a28
9 files changed
+170
-54
No files matched your search
@@ -1,7 +1,8 @@
|
||||
from pathlib import Path
|
||||
from pdfmajor.document.PDFParsingContext import PDFParsingContext
|
||||
from typing import BinaryIO
|
||||
from typing import BinaryIO, List
|
||||
|
||||
from pdfmajor.document.pages import PDFPageTreeNode
|
||||
from pdfmajor.document.PDFParsingContext import PDFParsingContext
|
||||
from pdfmajor.streambuffer import BufferStream
|
||||
|
||||
|
||||
@@ -24,6 +25,7 @@ class PDFDocument:
|
||||
self.__parser = PDFParsingContext(buffer)
|
||||
self.catalog = self.__parser.get_catalog()
|
||||
self.info = self.__parser.get_info()
|
||||
self.pages: List[PDFPageTreeNode] = []
|
||||
|
||||
@property
|
||||
def health_report(self):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import Any, Dict, List, NamedTuple, Optional
|
||||
from typing import Any, Dict, NamedTuple, Optional
|
||||
|
||||
from pdfmajor.document.pages import PDFPageTreeNode
|
||||
from pdfmajor.parser.objects.collections import PDFDictionary
|
||||
from pdfmajor.parser.objects.indirect import ObjectRef
|
||||
from pdfmajor.parser.objects.primitives import PDFName
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ class PDFDocumentCatalog(NamedTuple):
|
||||
"""A Parsed version of the PDF Catalog object (see PDF spec 1.7 section 7.7.2 for more detail)"""
|
||||
|
||||
version: Optional[PDFName]
|
||||
pages: List[ObjectRef]
|
||||
pages: PDFPageTreeNode
|
||||
page_labels: Optional[PDFDictionary]
|
||||
page_layout: Optional[PDFName]
|
||||
page_mode: Optional[PDFName]
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, BinaryIO, Dict, Optional, Set, Tuple, Type, TypeVar
|
||||
|
||||
from pdfmajor.document.pages import PDFPageTreeNode
|
||||
from pdfmajor.healthlog import PDFHealthReport
|
||||
from pdfmajor.parser.objects.base import PDFObject
|
||||
from pdfmajor.parser.objects.collections import PDFArray, PDFDictionary
|
||||
from pdfmajor.parser.objects.collections import PDFDictionary
|
||||
from pdfmajor.parser.objects.indirect import IndirectObject, ObjectRef
|
||||
from pdfmajor.parser.objects.primitives import PDFName
|
||||
from pdfmajor.streambuffer import BufferStream
|
||||
@@ -34,6 +35,14 @@ class PDFParsingContext:
|
||||
self.xrefdb = XRefDB(buffer)
|
||||
|
||||
def get_object_from_ref(self, objref: ObjectRef) -> IndirectObject:
|
||||
"""Get an indirect object from it's object reference
|
||||
|
||||
Args:
|
||||
objref (ObjectRef)
|
||||
|
||||
Returns:
|
||||
IndirectObject
|
||||
"""
|
||||
with self.buffer.get_window():
|
||||
return self.xrefdb.get_obj(
|
||||
objref.obj_num, objref.gen_num, buffer=self.buffer
|
||||
@@ -126,20 +135,18 @@ class PDFParsingContext:
|
||||
pages_obj = cat_obj["Pages"]
|
||||
except KeyError:
|
||||
raise InvalidCatalogObj(f"Missing Pages entry {cat_obj}")
|
||||
pages = []
|
||||
if isinstance(pages_obj, ObjectRef):
|
||||
pages = [pages_obj]
|
||||
elif not isinstance(pages_obj, PDFArray):
|
||||
raise InvalidCatalogObj(f"Invalid pages entry {pages_obj}")
|
||||
pages = self.get_object_from_ref(pages_obj).get_object()
|
||||
if not isinstance(pages, PDFDictionary):
|
||||
raise InvalidCatalogObj(
|
||||
f"Invalid pages ref entry {pages} from {pages_obj}"
|
||||
)
|
||||
elif isinstance(pages_obj, PDFDictionary):
|
||||
pages = pages_obj
|
||||
else:
|
||||
for page in pages_obj:
|
||||
if isinstance(page, ObjectRef):
|
||||
pages.append(page)
|
||||
else:
|
||||
self.health_report.write(
|
||||
"InvalidPageRef", f"The page {page} has an invalid value"
|
||||
)
|
||||
validated_fields["pages"] = pages
|
||||
raise InvalidCatalogObj(f"Invalid pages entry {pages_obj}")
|
||||
|
||||
validated_fields["pages"] = PDFPageTreeNode.from_pdfdict(pages)
|
||||
validated_fields["raw"] = cat_obj
|
||||
return PDFDocumentCatalog(**validated_fields)
|
||||
|
||||
|
||||
@@ -31,3 +31,7 @@ class TooManyRectField(BrokenFilePDF):
|
||||
def __init__(self, values: List[Decimal]) -> None:
|
||||
super().__init__(f"Too many elements found for rectangle | values={values}")
|
||||
self.values = values
|
||||
|
||||
|
||||
class InvalidPagesNodeKids(BrokenFilePDF):
|
||||
pass
|
||||
@@ -1,13 +1,45 @@
|
||||
from decimal import Decimal
|
||||
from typing import List, NamedTuple, Optional
|
||||
|
||||
from pdfmajor.document.exceptions import InvalidPagesNodeKids
|
||||
from pdfmajor.document.structures import PDFRectangle
|
||||
from pdfmajor.parser.objects.collections import PDFArray, PDFDictionary
|
||||
from pdfmajor.parser.objects.indirect import ObjectRef
|
||||
from pdfmajor.parser.objects.collections import PDFDictionary
|
||||
from pdfmajor.parser.objects.primitives import PDFInteger
|
||||
from pdfmajor.parser.stream.PDFStream import PDFStream
|
||||
from pdfmajor.util import validate_object_or_none
|
||||
|
||||
|
||||
class PDFPageTreeNode(NamedTuple):
|
||||
"""A PDF Pages Node representation as it confirms with PDF spec 1.7 section 7.7.3.2"""
|
||||
|
||||
kids: List[ObjectRef]
|
||||
parent: Optional[ObjectRef] = None
|
||||
parent: Optional[ObjectRef]
|
||||
leaft_count: int
|
||||
|
||||
raw: PDFDictionary
|
||||
|
||||
@classmethod
|
||||
def from_pdfdict(cls, pdfdict: PDFDictionary):
|
||||
parent = validate_object_or_none(pdfdict.get("Parent"), ObjectRef)
|
||||
kids = pdfdict["Kids"]
|
||||
count = validate_object_or_none(pdfdict.get("Count"), PDFInteger)
|
||||
vetted_kids: List[ObjectRef] = []
|
||||
if isinstance(kids, ObjectRef):
|
||||
vetted_kids = [kids]
|
||||
elif isinstance(kids, PDFArray):
|
||||
for kid in kids:
|
||||
vetted_kid = validate_object_or_none(kid, ObjectRef)
|
||||
if vetted_kid:
|
||||
vetted_kids.append(vetted_kid)
|
||||
else:
|
||||
raise InvalidPagesNodeKids(f"{kids}")
|
||||
return cls(
|
||||
kids=vetted_kids,
|
||||
parent=parent,
|
||||
raw=pdfdict,
|
||||
leaft_count=0 if count is None else count.to_python(),
|
||||
)
|
||||
|
||||
|
||||
class PDFPage(NamedTuple):
|
||||
@@ -15,3 +47,11 @@ class PDFPage(NamedTuple):
|
||||
|
||||
parent: ObjectRef
|
||||
resources: PDFDictionary
|
||||
mediabox: PDFRectangle
|
||||
bleedbox: PDFRectangle
|
||||
trimbox: PDFRectangle
|
||||
contents: List[PDFStream]
|
||||
boxcolor_info: Optional[PDFDictionary]
|
||||
rotate: Decimal
|
||||
metadata: Optional[ObjectRef]
|
||||
user_unit: Decimal
|
||||
@@ -1,8 +1,9 @@
|
||||
from decimal import Decimal
|
||||
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
|
||||
from pdfmajor.parser.objects.primitives import PDFInteger, PDFReal
|
||||
from typing import List, NamedTuple
|
||||
|
||||
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
|
||||
from pdfmajor.parser.objects.collections import PDFArray
|
||||
from pdfmajor.parser.objects.primitives import PDFInteger, PDFReal
|
||||
|
||||
|
||||
class PDFRectangle(NamedTuple):
|
||||
|
||||
@@ -31,6 +31,9 @@ class PDFDictionary(Dict[str, PDFObject], PDFContextualObject):
|
||||
self.last_name: Optional[PDFName] = None
|
||||
self.strict = strict
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"PDFDictionary({self.to_python()})"
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, d: Dict[str, PDFObject]):
|
||||
new_obj = cls()
|
||||
|
||||
@@ -107,6 +107,19 @@ class XRefDB:
|
||||
gen_num: GenNum,
|
||||
buffer: BufferStream,
|
||||
) -> IndirectObject:
|
||||
"""Get's an indirect object from either the pdf stream or a cached version in memeory
|
||||
|
||||
Args:
|
||||
obj_num (ObjNum)
|
||||
gen_num (GenNum)
|
||||
buffer (BufferStream)
|
||||
|
||||
Raises:
|
||||
InvalidXref: if the reference returned was not an indirect object
|
||||
|
||||
Returns:
|
||||
IndirectObject
|
||||
"""
|
||||
raw_ref, xref = (obj_num, gen_num), self.xrefs[(obj_num, gen_num)]
|
||||
obj = self.objs.get(raw_ref, None)
|
||||
if obj is None:
|
||||
|
||||
+78
-32
@@ -1,13 +1,14 @@
|
||||
from decimal import Decimal
|
||||
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
|
||||
from typing import List
|
||||
from pdfmajor.parser.objects.base import PDFObject
|
||||
from pdfmajor.parser.objects.collections import PDFArray
|
||||
from pdfmajor.document.structures import PDFRectangle
|
||||
from unittest import TestCase
|
||||
|
||||
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
|
||||
from pdfmajor.document.pages import PDFPageTreeNode
|
||||
from pdfmajor.document.PDFDocumentCatalog import PDFDocumentCatalog
|
||||
from pdfmajor.document.PDFParsingContext import PDFParsingContext
|
||||
from pdfmajor.document.structures import PDFRectangle
|
||||
from pdfmajor.parser.objects.base import PDFObject
|
||||
from pdfmajor.parser.objects.collections import PDFArray, PDFDictionary
|
||||
from pdfmajor.parser.objects.indirect import ObjectRef
|
||||
from pdfmajor.parser.objects.primitives import (
|
||||
PDFBoolean,
|
||||
@@ -23,8 +24,10 @@ class ParsingState(TestCase):
|
||||
def run_test(self, file_name: str, expcat: PDFDocumentCatalog):
|
||||
with all_pdf_files[file_name].get_window() as buffer:
|
||||
pdf = PDFParsingContext(buffer)
|
||||
self.assertEqual(len(pdf.health_report), 0)
|
||||
cat = pdf.get_catalog()
|
||||
self.assertEqual(
|
||||
pdf.get_catalog(),
|
||||
cat,
|
||||
expcat,
|
||||
)
|
||||
|
||||
@@ -33,21 +36,41 @@ class ParsingState(TestCase):
|
||||
"bad-unicode.pdf",
|
||||
PDFDocumentCatalog(
|
||||
version=None,
|
||||
pages=[ObjectRef(141, 0)],
|
||||
pages=PDFPageTreeNode.from_pdfdict(
|
||||
PDFDictionary.from_dict(
|
||||
{
|
||||
"Count": PDFInteger(47, 0, 0),
|
||||
"Type": PDFName("Pages", 0, 0),
|
||||
"Kids": PDFArray.from_list(
|
||||
[
|
||||
ObjectRef(142, 0),
|
||||
ObjectRef(143, 0),
|
||||
ObjectRef(144, 0),
|
||||
ObjectRef(145, 0),
|
||||
ObjectRef(146, 0),
|
||||
]
|
||||
),
|
||||
}
|
||||
)
|
||||
),
|
||||
page_labels={"Nums": [PDFInteger(0, 0, 0), ObjectRef(140, 0)]},
|
||||
page_layout=None,
|
||||
page_mode=None,
|
||||
metadata={
|
||||
"Subtype": PDFName("XML", 0, 0),
|
||||
"Length": PDFInteger(3529, 0, 0),
|
||||
"Type": PDFName("Metadata", 0, 0),
|
||||
},
|
||||
raw={
|
||||
"Metadata": ObjectRef(147, 0),
|
||||
"Pages": ObjectRef(141, 0),
|
||||
"Type": PDFName("Catalog", 0, 0),
|
||||
"PageLabels": ObjectRef(139, 0),
|
||||
},
|
||||
metadata=PDFDictionary.from_dict(
|
||||
{
|
||||
"Subtype": PDFName("XML", 0, 0),
|
||||
"Length": PDFInteger(3529, 0, 0),
|
||||
"Type": PDFName("Metadata", 0, 0),
|
||||
}
|
||||
),
|
||||
raw=PDFDictionary.from_dict(
|
||||
{
|
||||
"Metadata": ObjectRef(147, 0),
|
||||
"Pages": ObjectRef(141, 0),
|
||||
"Type": PDFName("Catalog", 0, 0),
|
||||
"PageLabels": ObjectRef(139, 0),
|
||||
}
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -56,24 +79,47 @@ class ParsingState(TestCase):
|
||||
"bar-charts.pdf",
|
||||
PDFDocumentCatalog(
|
||||
version=None,
|
||||
pages=[ObjectRef(2, 0)],
|
||||
pages=PDFPageTreeNode.from_pdfdict(
|
||||
PDFDictionary.from_dict(
|
||||
{
|
||||
"Count": PDFInteger(6, 0, 0),
|
||||
"Type": PDFName("Pages", 0, 0),
|
||||
"Kids": PDFArray(
|
||||
[
|
||||
ObjectRef(3, 0),
|
||||
ObjectRef(9, 0),
|
||||
ObjectRef(13, 0),
|
||||
ObjectRef(36, 0),
|
||||
ObjectRef(66, 0),
|
||||
ObjectRef(188, 0),
|
||||
]
|
||||
),
|
||||
}
|
||||
)
|
||||
),
|
||||
page_labels=None,
|
||||
page_layout=None,
|
||||
page_mode=None,
|
||||
metadata={
|
||||
"Type": PDFName("Metadata", 0, 0),
|
||||
"Subtype": PDFName("XML", 0, 0),
|
||||
"Length": PDFInteger(3065, 0, 0),
|
||||
},
|
||||
raw={
|
||||
"Type": PDFName("Catalog", 0, 0),
|
||||
"Pages": ObjectRef(2, 0),
|
||||
"Lang": PDFString(b"en-US", 0, 0),
|
||||
"StructTreeRoot": ObjectRef(283, 0),
|
||||
"MarkInfo": {"Marked": PDFBoolean(True, 0, 0)},
|
||||
"Metadata": ObjectRef(646, 0),
|
||||
"ViewerPreferences": ObjectRef(647, 0),
|
||||
},
|
||||
metadata=PDFDictionary.from_dict(
|
||||
{
|
||||
"Type": PDFName("Metadata", 0, 0),
|
||||
"Subtype": PDFName("XML", 0, 0),
|
||||
"Length": PDFInteger(3065, 0, 0),
|
||||
}
|
||||
),
|
||||
raw=PDFDictionary.from_dict(
|
||||
{
|
||||
"Type": PDFName("Catalog", 0, 0),
|
||||
"Pages": ObjectRef(2, 0),
|
||||
"Lang": PDFString(b"en-US", 0, 0),
|
||||
"StructTreeRoot": ObjectRef(283, 0),
|
||||
"MarkInfo": PDFDictionary.from_dict(
|
||||
{"Marked": PDFBoolean(True, 0, 0)}
|
||||
),
|
||||
"Metadata": ObjectRef(646, 0),
|
||||
"ViewerPreferences": ObjectRef(647, 0),
|
||||
}
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user