added page counts

This commit is contained in:
ari committed 2020-11-02 13:21:41 -05:00
1 parent b48d635ec7
commit 76d64a2f20
3 files changed
+55 -10

No files matched your search

+40 -2
View File
@@ -1,7 +1,8 @@
from pathlib import Path
from typing import BinaryIO, List
from typing import BinaryIO, Iterator, List, Optional
from pdfmajor.document.pages import PDFPage
from pdfmajor.document.parsers.pages import iter_all_page_leafs
from pdfmajor.document.parsers.root import get_catalog, get_info
from pdfmajor.document.PDFParsingContext import PDFParsingContext
from pdfmajor.streambuffer import BufferStream
@@ -24,11 +25,48 @@ class PDFDocument:
def __init__(self, buffer: BufferStream) -> None:
self.__parser = PDFParsingContext(buffer)
self.__processed_pages: Optional[List[PDFPage]] = None
self.__processed_pages_done = False
self.catalog = get_catalog(self.__parser)
self.info = get_info(self.__parser)
self.pages: List[PDFPage] = []
@property
def health_report(self):
"""A log of errors that occured in the document during the processing of the file"""
return self.__parser.health_report
@property
def num_pages(self) -> int:
"""Returns the number of pages that the metadata of the document claims it has.
This number should be trusted, unless you suspect the document may have encoutered some corruption. To get an excat count do `len(list(self.iter_pages()))`. Please note that this will load all of the metadata of each document into memory.
Returns:
int
"""
return self.catalog.pages.leaft_count
def iter_pages(self) -> Iterator[PDFPage]:
"""Iterate over all of the pages in the document, this will also keep a record of the pdf page in memory for quicker future retrieval! (use self.clear() to free it up)
Yields:
Iterator[PDFPage]
"""
if self.__processed_pages is not None and self.__processed_pages_done:
return iter(self.__processed_pages)
else:
self.__processed_pages = []
for page in iter_all_page_leafs(self.__parser, self.catalog.pages):
self.__processed_pages.append(page)
yield page
self.__processed_pages_done = True
if len(self.__processed_pages) != self.num_pages:
print(
"WARN! number of pages proceseed with iter_pages did not match number of pages specified by the document's metadata!"
f"\n --> expected pages = {self.num_pages}"
f"\n --> found pages = {len(self.__processed_pages)}"
)
def clear(self):
"""Clears out memory used by the class as much as possible"""
self.__processed_pages = None
self.__processed_pages_done = False
+13 -6
View File
@@ -1,5 +1,5 @@
from decimal import Decimal
from typing import Any, Dict, List, Optional
from typing import Any, Dict, Iterator, List, Optional
from pdfmajor.document.exceptions import BrokenFilePDF
from pdfmajor.parser.objects.base import PDFObject
@@ -16,10 +16,18 @@ from ..utils import iter_single_ref_as_array_ref
PDFRawFields = Dict[str, Any]
def get_all_page_leafs(
def iter_all_page_leafs(
pctx: PDFParsingContext, root_node: PDFPageTreeNode
) -> List[PDFPage]:
all_leafs: List[PDFPage] = []
) -> Iterator[PDFPage]:
"""Iterate over every page in the pdf
Args:
pctx (PDFParsingContext)
root_node (PDFPageTreeNode)
Yields:
Iterator[PDFPage]
"""
current_root: List[PDFPageTreeNode] = [root_node]
current_root_aspage: List[Dict[str, Optional[PDFObject]]] = [
parse_pdfpage_fields(pctx, root_node.raw, allow_missing=True)
@@ -53,7 +61,7 @@ def get_all_page_leafs(
current_child_idx.append(0)
continue
elif obj["Type"].to_python() == "/Page":
all_leafs.append(make_pdfpage(croot_pagedata, pctx, obj))
yield make_pdfpage(croot_pagedata, pctx, obj)
current_child_idx[-1] += 1
continue
# catch all other cases (the above expression will issue a 'continue')
@@ -62,7 +70,6 @@ def get_all_page_leafs(
f"Expecting page ref {page_ref} to be a dictionary with type 'Pages' or 'Page', but got a {obj}",
)
current_child_idx[-1] += 1
return all_leafs
def make_pdfpage(
+2 -2
View File
@@ -4,7 +4,7 @@ from unittest import TestCase
from pdfmajor.document.exceptions import BrokenFilePDF, TooManyRectField
from pdfmajor.document.pages import PDFPageTreeNode
from pdfmajor.document.parsers.pages import get_all_page_leafs
from pdfmajor.document.parsers.pages import iter_all_page_leafs
from pdfmajor.document.parsers.root import get_catalog, get_info
from pdfmajor.document.PDFDocument import PDFDocument
from pdfmajor.document.PDFDocumentCatalog import PDFDocumentCatalog
@@ -133,7 +133,7 @@ class ParsingState(TestCase):
catalog = get_catalog(parser)
self.assertEqual(len(parser.health_report), 0)
get_info(parser)
get_all_page_leafs(parser, catalog.pages)
list(iter_all_page_leafs(parser, catalog.pages))
def test_doc(self):
with all_pdf_files["bad-unicode.pdf"].get_window() as buffer: