Prevent Ghostscript from generating invalid XMP metadata

If DocumentInfo contains NULs Ghostscript will generate XMP with
NULs which is not allowed. Repair DocumentInfo before Ghostscript sees it.
This commit is contained in:
James R. Barlow
2019-01-04 13:20:41 -08:00
parent 089ece2715
commit f34b3015b2
7 changed files with 71 additions and 2 deletions
+17
View File
@@ -810,6 +810,23 @@ def convert_to_pdfa(input_files_groups, output_file, log, context):
layers_file = next(
(ii for ii in input_files if ii.endswith('layers.rendered.pdf')), None
)
# If the DocumentInfo record contains NUL characters, Ghostscript will
# produce XMP metadata which contains invalid XML entities (�).
# NULs in DocumentInfo seem to be common since older Acrobats included them.
# pikepdf can deal with this, but we make the world a better place by
# stamping them out as soon as possible.
pdf_layers_file = pikepdf.open(layers_file)
if pdf_layers_file.docinfo:
modified = False
for k, v in pdf_layers_file.docinfo.items():
if b'\x00' in bytes(v):
pdf_layers_file.docinfo[k] = bytes(v).replace(b'\x00', b'')
modified = True
if modified:
pdf_layers_file.save(layers_file)
del pdf_layers_file
ps = next((ii for ii in input_files if ii.endswith('.ps')), None)
ghostscript.generate_pdfa(
pdf_version=input_pdfinfo.min_version,