warn on unicode decoding errors in PDF annotations

in some cases the the annotations may contain some junk that hinders annotations processing altogether. This allows to ignore the error and warn instead, which is configurable via warn_unicode_error arguments in the PDF initializer and/or open() method.
This commit is contained in:
Michal Stolarczyk
2024-08-30 10:24:39 +02:00
parent e921ea748b
commit 396c5e3669
2 changed files with 11 additions and 1 deletions
+7 -1
View File
@@ -13,6 +13,7 @@ from typing import (
Union,
)
from unicodedata import normalize as normalize_unicode
from warnings import warn
from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import (
@@ -306,7 +307,12 @@ class Page(Container):
try:
extras[k] = v.decode("utf-8")
except UnicodeDecodeError:
extras[k] = v.decode("utf-16")
try:
extras[k] = v.decode("utf-16")
except UnicodeDecodeError:
if not self.pdf.warn_unicode_error:
raise
warn(f"Could not decode {k} for annotation. {k} will be missing.")
parsed = {
"page_number": self.page_number,
+4
View File
@@ -35,6 +35,7 @@ class PDF(Container):
password: Optional[str] = None,
strict_metadata: bool = False,
unicode_norm: Optional[Literal["NFC", "NFKC", "NFD", "NFKD"]] = None,
warn_unicode_error: bool = False,
):
self.stream = stream
self.stream_is_external = stream_is_external
@@ -43,6 +44,7 @@ class PDF(Container):
self.laparams = None if laparams is None else LAParams(**laparams)
self.password = password
self.unicode_norm = unicode_norm
self.warn_unicode_error = warn_unicode_error
self.doc = PDFDocument(PDFParser(stream), password=password or "")
self.rsrcmgr = PDFResourceManager()
@@ -76,6 +78,7 @@ class PDF(Container):
repair: bool = False,
gs_path: Optional[Union[str, pathlib.Path]] = None,
repair_setting: T_repair_setting = "default",
warn_unicode_error: bool = False,
) -> "PDF":
stream: Union[BufferedReader, BytesIO]
@@ -107,6 +110,7 @@ class PDF(Container):
strict_metadata=strict_metadata,
unicode_norm=unicode_norm,
stream_is_external=stream_is_external,
warn_unicode_error=warn_unicode_error,
)
except PSException: