Handle non-UTF-8 DocumentInfo keys in repair_docinfo_nuls (closes #1540)

Some PDFs use a /Name dictionary key in /DocumentInfo whose bytes are not
valid UTF-8/PDFDocEncoding, e.g. a Latin-1 /Saks#e5r. Older pikepdf raised
UnicodeDecodeError while iterating such a block, crashing the pipeline
during PDF/A conversion. repair_docinfo_nuls is documented to log and
continue on a malformed DocumentInfo block, so catch UnicodeDecodeError
alongside TypeError.

Add a mock-based unit test that drives the decode-error branch (current
pikepdf surrogate-escapes instead of raising) and an end-to-end test over
the reporter's file, committed as docinfo_latin1_key.pdf.
This commit is contained in:
James R. Barlow
2026-06-27 01:50:58 -07:00
parent 8b20bb3c5b
commit ea7ad7d683
5 changed files with 79 additions and 3 deletions
+6 -2
View File
@@ -88,8 +88,12 @@ def repair_docinfo_nuls(pdf):
if isinstance(v, str) and b'\x00' in bytes(v):
pdf.docinfo[k] = bytes(v).replace(b'\x00', b'')
modified = True
except TypeError:
# TypeError can also be raised if dictionary items are unexpected types
except (TypeError, UnicodeDecodeError):
# TypeError: DocumentInfo is not a dictionary, or its items are
# unexpected types.
# UnicodeDecodeError: a DocumentInfo key or value contains bytes that
# are not valid PDFDocEncoding/UTF-16, e.g. a Latin-1 /Name key such as
# /Saks#e5r. Older pikepdf raised while iterating such a block (#1540).
log.error("File contains a malformed DocumentInfo block - continuing anyway.")
return modified