2022-07-28 01:06:46 -07:00
|
|
|
# SPDX-FileCopyrightText: 2022 James R. Barlow
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
2018-03-26 01:49:25 -07:00
|
|
|
|
2022-07-23 00:39:24 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-12-30 01:28:15 -08:00
|
|
|
import datetime
|
2022-06-01 00:46:16 -07:00
|
|
|
import warnings
|
2019-12-19 15:29:56 -08:00
|
|
|
from datetime import timezone
|
2020-05-03 00:51:17 -07:00
|
|
|
from shutil import copyfile
|
2018-03-26 01:49:25 -07:00
|
|
|
|
2019-12-19 15:29:56 -08:00
|
|
|
import pikepdf
|
2018-12-30 01:28:15 -08:00
|
|
|
import pytest
|
2023-09-25 00:59:16 -07:00
|
|
|
from packaging.version import Version
|
2019-12-19 15:29:56 -08:00
|
|
|
from pikepdf.models.metadata import decode_pdf_date
|
2018-09-10 16:06:01 -07:00
|
|
|
|
2023-09-25 00:59:16 -07:00
|
|
|
from ocrmypdf._exec import ghostscript
|
2020-05-02 04:32:46 -07:00
|
|
|
from ocrmypdf._jobcontext import PdfContext
|
2023-10-12 15:52:19 -07:00
|
|
|
from ocrmypdf._metadata import metadata_fixup
|
|
|
|
from ocrmypdf._pipeline import convert_to_pdfa
|
2022-06-13 00:59:41 -07:00
|
|
|
from ocrmypdf._plugin_manager import get_parser_options_plugins, get_plugin_manager
|
2018-03-26 01:49:25 -07:00
|
|
|
from ocrmypdf.exceptions import ExitCode
|
2021-11-13 00:50:10 -08:00
|
|
|
from ocrmypdf.pdfa import file_claims_pdfa, generate_pdfa_ps
|
2020-03-03 03:26:46 -08:00
|
|
|
from ocrmypdf.pdfinfo import PdfInfo
|
2018-06-13 01:02:53 -07:00
|
|
|
|
2023-10-16 00:06:15 -07:00
|
|
|
from .conftest import check_ocrmypdf, run_ocrmypdf, run_ocrmypdf_api
|
2021-04-07 01:56:51 -07:00
|
|
|
|
2018-03-26 01:49:25 -07:00
|
|
|
|
2018-12-30 01:27:49 -08:00
|
|
|
@pytest.mark.parametrize("output_type", ['pdfa', 'pdf'])
|
2020-06-20 02:01:16 -07:00
|
|
|
def test_preserve_docinfo(output_type, resources, outpdf):
|
2018-03-26 01:49:25 -07:00
|
|
|
output = check_ocrmypdf(
|
2018-12-30 01:27:49 -08:00
|
|
|
resources / 'graph.pdf',
|
|
|
|
outpdf,
|
|
|
|
'--output-type',
|
|
|
|
output_type,
|
2020-06-01 03:06:40 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2024-04-07 00:25:32 -07:00
|
|
|
with (
|
|
|
|
pikepdf.open(resources / 'graph.pdf') as pdf_before,
|
|
|
|
pikepdf.open(output) as pdf_after,
|
|
|
|
):
|
2023-04-15 20:17:44 -07:00
|
|
|
for key in ('/Title', '/Author'):
|
|
|
|
assert pdf_before.docinfo[key] == pdf_after.docinfo[key]
|
|
|
|
pdfa_info = file_claims_pdfa(str(output))
|
|
|
|
assert pdfa_info['output'] == output_type
|
2018-03-26 01:49:25 -07:00
|
|
|
|
|
|
|
|
2018-12-30 01:27:49 -08:00
|
|
|
@pytest.mark.parametrize("output_type", ['pdfa', 'pdf'])
|
2023-10-16 00:06:15 -07:00
|
|
|
def test_override_metadata(output_type, resources, outpdf, caplog):
|
2018-03-26 01:49:25 -07:00
|
|
|
input_file = resources / 'c02-22.pdf'
|
|
|
|
german = 'Du siehst den Wald vor lauter Bäumen nicht.'
|
|
|
|
chinese = '孔子'
|
|
|
|
|
2023-10-16 00:06:15 -07:00
|
|
|
exitcode = run_ocrmypdf_api(
|
2018-12-30 01:27:49 -08:00
|
|
|
input_file,
|
|
|
|
outpdf,
|
|
|
|
'--title',
|
|
|
|
german,
|
|
|
|
'--author',
|
|
|
|
chinese,
|
|
|
|
'--output-type',
|
|
|
|
output_type,
|
2020-06-01 03:06:40 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2018-03-26 01:49:25 -07:00
|
|
|
|
2023-10-16 00:06:15 -07:00
|
|
|
assert exitcode == ExitCode.ok, caplog.text
|
2018-03-26 01:49:25 -07:00
|
|
|
|
2023-04-15 17:56:13 -07:00
|
|
|
with pikepdf.open(input_file) as before, pikepdf.open(outpdf) as after:
|
|
|
|
assert after.docinfo.Title == german, after.docinfo
|
|
|
|
assert after.docinfo.Author == chinese, after.docinfo
|
|
|
|
assert after.docinfo.get('/Keywords', '') == ''
|
2018-05-03 16:30:20 -07:00
|
|
|
|
2023-04-15 17:56:13 -07:00
|
|
|
before_date = decode_pdf_date(str(before.docinfo.CreationDate))
|
|
|
|
after_date = decode_pdf_date(str(after.docinfo.CreationDate))
|
|
|
|
assert before_date == after_date
|
2018-03-26 01:49:25 -07:00
|
|
|
|
2023-04-15 17:56:13 -07:00
|
|
|
pdfa_info = file_claims_pdfa(outpdf)
|
|
|
|
assert pdfa_info['output'] == output_type
|
2018-03-26 01:49:25 -07:00
|
|
|
|
|
|
|
|
2023-06-20 04:07:23 -04:00
|
|
|
@pytest.mark.parametrize('output_type', ['pdfa', 'pdf', 'pdfa-1', 'pdfa-2', 'pdfa-3'])
|
|
|
|
@pytest.mark.parametrize('field', ['title', 'author', 'subject', 'keywords'])
|
2023-10-16 00:06:15 -07:00
|
|
|
def test_unset_metadata(output_type, field, resources, outpdf, caplog):
|
2023-06-20 04:07:23 -04:00
|
|
|
input_file = resources / 'meta.pdf'
|
|
|
|
|
|
|
|
# magic strings contained in the input pdf metadata
|
|
|
|
meta = {
|
|
|
|
'title': b'NFY5f7Ft2DWMkxLhXwxvFf7eWR2KeK3vEDcd',
|
|
|
|
'author': b'yXaryipxyRk9dVjWjSSaVaNCKeLRgEVzPRMp',
|
|
|
|
'subject': b't49vimctvnuH7ZeAjAkv52ACvWFjcnm5MPJr',
|
2023-09-20 01:08:15 -07:00
|
|
|
'keywords': b's9EeALwUg7urA7fnnhm5EtUyC54sW2WPUzqh',
|
|
|
|
}
|
2023-06-20 04:07:23 -04:00
|
|
|
|
2023-10-16 00:06:15 -07:00
|
|
|
exitcode = run_ocrmypdf_api(
|
2023-06-20 04:07:23 -04:00
|
|
|
input_file,
|
|
|
|
outpdf,
|
|
|
|
f'--{field}',
|
|
|
|
'',
|
|
|
|
'--output-type',
|
|
|
|
output_type,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
|
|
|
)
|
|
|
|
|
2023-10-16 00:06:15 -07:00
|
|
|
assert exitcode == ExitCode.ok, caplog.text
|
2023-06-20 04:07:23 -04:00
|
|
|
|
|
|
|
# We mainly want to ensure that when '' is passed, the corresponding
|
|
|
|
# metadata is unset in the output pdf. Since metedata is not compressed,
|
|
|
|
# the best way to gaurentee the metadata of interest didn't carry
|
|
|
|
# forward is to just check to ensure the corresponding magic string
|
|
|
|
# isn't contained anywhere in the output pdf. We'll also check to ensure
|
|
|
|
# it's in the input pdf and that any values not unset are still in the
|
|
|
|
# output pdf.
|
|
|
|
with open(input_file, 'rb') as before, open(outpdf, 'rb') as after:
|
|
|
|
before_data = before.read()
|
|
|
|
after_data = after.read()
|
|
|
|
|
|
|
|
for k, v in meta.items():
|
|
|
|
assert v in before_data
|
|
|
|
if k == field:
|
|
|
|
assert v not in after_data
|
|
|
|
else:
|
|
|
|
assert v in after_data
|
|
|
|
|
|
|
|
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_high_unicode(resources, no_outpdf):
|
2018-03-26 01:49:25 -07:00
|
|
|
# Ghostscript doesn't support high Unicode, so neither do we, to be
|
|
|
|
# safe
|
|
|
|
input_file = resources / 'c02-22.pdf'
|
|
|
|
high_unicode = 'U+1030C is: 𐌌'
|
|
|
|
|
2021-12-06 17:00:25 -08:00
|
|
|
p = run_ocrmypdf(
|
2018-12-30 01:27:49 -08:00
|
|
|
input_file,
|
|
|
|
no_outpdf,
|
|
|
|
'--subject',
|
|
|
|
high_unicode,
|
|
|
|
'--output-type',
|
|
|
|
'pdfa',
|
2020-06-01 03:06:40 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2018-03-26 01:49:25 -07:00
|
|
|
|
2021-12-06 17:00:25 -08:00
|
|
|
assert p.returncode == ExitCode.bad_args, p.stderr
|
2018-03-26 02:23:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('ocr_option', ['--skip-text', '--force-ocr'])
|
|
|
|
@pytest.mark.parametrize('output_type', ['pdf', 'pdfa'])
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_bookmarks_preserved(output_type, ocr_option, resources, outpdf):
|
2023-04-15 17:56:13 -07:00
|
|
|
fitz = pytest.importorskip('fitz')
|
2018-03-26 02:23:19 -07:00
|
|
|
input_file = resources / 'toc.pdf'
|
2021-11-13 00:53:41 -08:00
|
|
|
before_toc = fitz.Document(str(input_file)).get_toc()
|
2018-03-26 02:23:19 -07:00
|
|
|
|
|
|
|
check_ocrmypdf(
|
2018-12-30 01:27:49 -08:00
|
|
|
input_file,
|
|
|
|
outpdf,
|
2018-03-26 02:23:19 -07:00
|
|
|
ocr_option,
|
2018-12-30 01:27:49 -08:00
|
|
|
'--output-type',
|
|
|
|
output_type,
|
2020-06-01 03:06:40 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2018-03-26 02:23:19 -07:00
|
|
|
|
2021-11-13 00:53:41 -08:00
|
|
|
after_toc = fitz.Document(str(outpdf)).get_toc()
|
2018-03-26 02:23:19 -07:00
|
|
|
print(before_toc)
|
|
|
|
print(after_toc)
|
|
|
|
assert before_toc == after_toc
|
2018-04-02 17:53:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
def seconds_between_dates(date1, date2):
|
|
|
|
return (date2 - date1).total_seconds()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('infile', ['trivial.pdf', 'jbig2.pdf'])
|
|
|
|
@pytest.mark.parametrize('output_type', ['pdf', 'pdfa'])
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_creation_date_preserved(output_type, resources, infile, outpdf):
|
2018-04-02 17:53:39 -07:00
|
|
|
input_file = resources / infile
|
|
|
|
|
|
|
|
check_ocrmypdf(
|
2020-06-01 03:06:40 -07:00
|
|
|
input_file,
|
|
|
|
outpdf,
|
|
|
|
'--output-type',
|
|
|
|
output_type,
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2018-09-10 16:06:01 -07:00
|
|
|
|
2023-04-15 20:17:44 -07:00
|
|
|
with pikepdf.open(input_file) as pdf_before, pikepdf.open(outpdf) as pdf_after:
|
|
|
|
before = pdf_before.trailer.get('/Info', {})
|
|
|
|
after = pdf_after.trailer.get('/Info', {})
|
2018-04-02 17:53:39 -07:00
|
|
|
|
2023-04-15 20:17:44 -07:00
|
|
|
if not before:
|
|
|
|
assert after.get('/CreationDate', '') != ''
|
|
|
|
else:
|
|
|
|
# We expect that the creation date stayed the same
|
|
|
|
date_before = decode_pdf_date(str(before['/CreationDate']))
|
|
|
|
date_after = decode_pdf_date(str(after['/CreationDate']))
|
|
|
|
assert seconds_between_dates(date_before, date_after) < 1000
|
2018-04-02 17:53:39 -07:00
|
|
|
|
2023-04-15 20:17:44 -07:00
|
|
|
# We expect that the modified date is quite recent
|
|
|
|
date_after = decode_pdf_date(str(after['/ModDate']))
|
|
|
|
assert (
|
|
|
|
seconds_between_dates(date_after, datetime.datetime.now(timezone.utc))
|
|
|
|
< 1000
|
|
|
|
)
|
2018-04-02 17:53:39 -07:00
|
|
|
|
2018-05-10 20:37:10 -07:00
|
|
|
|
2022-06-01 00:46:16 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def libxmp_file_to_dict():
|
|
|
|
try:
|
|
|
|
with warnings.catch_warnings():
|
2022-08-04 04:13:02 -07:00
|
|
|
# libxmp imports distutils.Version, which is deprecated
|
|
|
|
warnings.filterwarnings(
|
|
|
|
"ignore",
|
|
|
|
category=DeprecationWarning,
|
|
|
|
message=r".*distutils Version classes are deprecated.*",
|
|
|
|
)
|
2022-06-01 00:46:16 -07:00
|
|
|
from libxmp.utils import (
|
|
|
|
file_to_dict, # pylint: disable=import-outside-toplevel
|
|
|
|
)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
pytest.skip("libxmp not available or libexempi3 not installed")
|
|
|
|
return file_to_dict
|
|
|
|
|
|
|
|
|
2020-06-20 02:01:16 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'test_file,output_type',
|
|
|
|
[
|
|
|
|
('graph.pdf', 'pdf'), # PDF with full metadata
|
|
|
|
('graph.pdf', 'pdfa'), # PDF/A with full metadata
|
|
|
|
('overlay.pdf', 'pdfa'), # /Title()
|
|
|
|
('3small.pdf', 'pdfa'),
|
|
|
|
],
|
|
|
|
)
|
2022-06-01 00:46:16 -07:00
|
|
|
def test_xml_metadata_preserved(
|
|
|
|
libxmp_file_to_dict, test_file, output_type, resources, outpdf
|
|
|
|
):
|
2020-06-20 02:01:16 -07:00
|
|
|
input_file = resources / test_file
|
2018-05-10 20:37:10 -07:00
|
|
|
|
2022-06-01 00:46:16 -07:00
|
|
|
before = libxmp_file_to_dict(str(input_file))
|
2018-05-10 16:43:28 -07:00
|
|
|
|
|
|
|
check_ocrmypdf(
|
2020-06-01 03:06:40 -07:00
|
|
|
input_file,
|
|
|
|
outpdf,
|
|
|
|
'--output-type',
|
|
|
|
output_type,
|
2020-06-20 02:01:16 -07:00
|
|
|
'--skip-text',
|
2020-06-01 03:06:40 -07:00
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2018-05-10 16:43:28 -07:00
|
|
|
|
2022-06-01 00:46:16 -07:00
|
|
|
after = libxmp_file_to_dict(str(outpdf))
|
2018-05-10 20:37:10 -07:00
|
|
|
|
2018-05-10 16:43:28 -07:00
|
|
|
equal_properties = [
|
2018-05-10 20:37:10 -07:00
|
|
|
'dc:contributor',
|
|
|
|
'dc:coverage',
|
|
|
|
'dc:creator',
|
|
|
|
'dc:description',
|
|
|
|
'dc:format',
|
|
|
|
'dc:identifier',
|
|
|
|
'dc:language',
|
|
|
|
'dc:publisher',
|
|
|
|
'dc:relation',
|
|
|
|
'dc:rights',
|
|
|
|
'dc:source',
|
|
|
|
'dc:subject',
|
|
|
|
'dc:title',
|
|
|
|
'dc:type',
|
|
|
|
'pdf:keywords',
|
2018-05-10 16:43:28 -07:00
|
|
|
]
|
2020-06-20 02:01:16 -07:00
|
|
|
acquired_properties = ['dc:format']
|
2018-05-10 16:43:28 -07:00
|
|
|
|
2018-05-10 20:37:10 -07:00
|
|
|
# Cleanup messy data structure
|
|
|
|
# Top level is key-value mapping of namespaces to keys under namespace,
|
|
|
|
# so we put everything in the same namespace
|
|
|
|
def unify_namespaces(xmpdict):
|
|
|
|
for entries in xmpdict.values():
|
|
|
|
yield from entries
|
2018-05-10 16:43:28 -07:00
|
|
|
|
2018-05-10 20:37:10 -07:00
|
|
|
# Now we have a list of (key, value, {infodict}). We don't care about
|
|
|
|
# infodict. Just flatten to keys and values
|
|
|
|
def keyval_from_tuple(list_of_tuples):
|
|
|
|
for k, v, *_ in list_of_tuples:
|
|
|
|
yield k, v
|
|
|
|
|
|
|
|
before = dict(keyval_from_tuple(unify_namespaces(before)))
|
|
|
|
after = dict(keyval_from_tuple(unify_namespaces(after)))
|
2018-05-10 16:43:28 -07:00
|
|
|
|
2018-05-10 20:37:10 -07:00
|
|
|
for prop in equal_properties:
|
|
|
|
if prop in before:
|
2018-12-31 15:00:02 -08:00
|
|
|
assert prop in after, f'{prop} dropped from xmp'
|
2018-05-10 20:37:10 -07:00
|
|
|
assert before[prop] == after[prop]
|
2018-05-17 00:14:57 -07:00
|
|
|
|
2020-06-20 02:01:16 -07:00
|
|
|
# libxmp presents multivalued entries (e.g. dc:title) as:
|
|
|
|
# 'dc:title': '' <- there's a title
|
|
|
|
# 'dc:title[1]: 'The Title' <- the actual title
|
|
|
|
# 'dc:title[1]/?xml:lang': 'x-default' <- language info
|
2018-12-31 15:00:02 -08:00
|
|
|
propidx = f'{prop}[1]'
|
2018-05-10 20:37:10 -07:00
|
|
|
if propidx in before:
|
2018-12-30 01:27:49 -08:00
|
|
|
assert (
|
|
|
|
after.get(propidx) == before[propidx]
|
|
|
|
or after.get(prop) == before[propidx]
|
|
|
|
)
|
2018-07-07 01:35:05 -07:00
|
|
|
|
2020-06-20 02:01:16 -07:00
|
|
|
if prop in after and prop not in before:
|
|
|
|
assert prop in acquired_properties, (
|
|
|
|
f"acquired unexpected property {prop} with value "
|
|
|
|
f"{after.get(propidx) or after.get(prop)}"
|
|
|
|
)
|
|
|
|
|
2018-07-07 01:35:05 -07:00
|
|
|
|
2020-06-01 03:06:40 -07:00
|
|
|
def test_kodak_toc(resources, outpdf):
|
2021-09-21 16:37:03 -07:00
|
|
|
check_ocrmypdf(
|
2020-06-01 03:06:40 -07:00
|
|
|
resources / 'kcs.pdf',
|
|
|
|
outpdf,
|
|
|
|
'--output-type',
|
|
|
|
'pdf',
|
|
|
|
'--plugin',
|
|
|
|
'tests/plugins/tesseract_noop.py',
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2018-09-11 14:44:16 -07:00
|
|
|
|
2023-04-15 20:17:44 -07:00
|
|
|
with pikepdf.open(outpdf) as p:
|
|
|
|
if pikepdf.Name.First in p.Root.Outlines:
|
|
|
|
assert isinstance(p.Root.Outlines.First, pikepdf.Dictionary)
|
2018-12-30 00:13:25 -08:00
|
|
|
|
|
|
|
|
2019-05-11 12:44:17 -07:00
|
|
|
def test_metadata_fixup_warning(resources, outdir, caplog):
|
2022-06-13 00:59:41 -07:00
|
|
|
_parser, options, _pm = get_parser_options_plugins(
|
|
|
|
['--output-type', 'pdfa-2', 'graph.pdf', 'out.pdf']
|
2018-12-30 01:27:49 -08:00
|
|
|
)
|
2019-04-08 14:57:42 +02:00
|
|
|
|
|
|
|
copyfile(resources / 'graph.pdf', outdir / 'graph.pdf')
|
2018-12-30 00:13:25 -08:00
|
|
|
|
2020-05-14 04:23:23 -07:00
|
|
|
context = PdfContext(
|
|
|
|
options, outdir, outdir / 'graph.pdf', None, get_plugin_manager([])
|
|
|
|
)
|
2023-10-12 15:52:19 -07:00
|
|
|
metadata_fixup(
|
|
|
|
working_file=outdir / 'graph.pdf', context=context, pdf_save_settings={}
|
|
|
|
)
|
2019-05-11 12:44:17 -07:00
|
|
|
for record in caplog.records:
|
2020-12-28 23:51:55 -08:00
|
|
|
assert record.levelname != 'WARNING', "Unexpected warning"
|
2018-12-30 00:13:25 -08:00
|
|
|
|
|
|
|
# Now add some metadata that will not be copyable
|
2023-04-15 20:17:44 -07:00
|
|
|
with pikepdf.open(outdir / 'graph.pdf') as graph:
|
|
|
|
with graph.open_metadata() as meta:
|
|
|
|
meta['prism2:publicationName'] = 'OCRmyPDF Test'
|
|
|
|
graph.save(outdir / 'graph_mod.pdf')
|
2018-12-30 00:13:25 -08:00
|
|
|
|
2020-05-14 04:23:23 -07:00
|
|
|
context = PdfContext(
|
|
|
|
options, outdir, outdir / 'graph_mod.pdf', None, get_plugin_manager([])
|
|
|
|
)
|
2023-10-12 15:52:19 -07:00
|
|
|
metadata_fixup(
|
|
|
|
working_file=outdir / 'graph.pdf', context=context, pdf_save_settings={}
|
|
|
|
)
|
2019-05-11 12:44:17 -07:00
|
|
|
assert any(record.levelname == 'WARNING' for record in caplog.records)
|
2019-01-04 13:20:41 -08:00
|
|
|
|
|
|
|
|
2022-06-11 01:15:30 -07:00
|
|
|
XMP_MAGIC = b'W5M0MpCehiHzreSzNTczkc9d'
|
|
|
|
|
|
|
|
|
2019-01-04 13:20:41 -08:00
|
|
|
def test_prevent_gs_invalid_xml(resources, outdir):
|
|
|
|
generate_pdfa_ps(outdir / 'pdfa.ps')
|
2020-02-12 00:07:18 -08:00
|
|
|
|
|
|
|
# Inject a string with a trailing nul character into the DocumentInfo
|
|
|
|
# dictionary of this PDF, as often occurs in practice.
|
2023-09-25 00:22:26 -07:00
|
|
|
with pikepdf.open(resources / 'trivial.pdf') as pdf:
|
|
|
|
pdf.Root.DocumentInfo = pikepdf.Dictionary(
|
2020-02-12 00:07:18 -08:00
|
|
|
Title=b'String with trailing nul\x00'
|
|
|
|
)
|
2023-09-25 00:22:26 -07:00
|
|
|
pdf.save(outdir / 'layers.rendered.pdf', fix_metadata_version=False)
|
2019-01-04 13:20:41 -08:00
|
|
|
|
2023-09-20 15:20:42 -07:00
|
|
|
_, options, _ = get_parser_options_plugins(
|
|
|
|
args=[
|
|
|
|
'-j',
|
|
|
|
'1',
|
|
|
|
'--output-type',
|
|
|
|
'pdfa-2',
|
|
|
|
'a.pdf',
|
|
|
|
'b.pdf',
|
|
|
|
]
|
2019-01-04 13:20:41 -08:00
|
|
|
)
|
2020-02-12 00:07:18 -08:00
|
|
|
pdfinfo = PdfInfo(outdir / 'layers.rendered.pdf')
|
2020-06-08 22:28:38 -07:00
|
|
|
context = PdfContext(
|
|
|
|
options, outdir, outdir / 'layers.rendered.pdf', pdfinfo, get_plugin_manager([])
|
|
|
|
)
|
2019-01-04 13:20:41 -08:00
|
|
|
|
|
|
|
convert_to_pdfa(
|
2019-05-14 16:32:34 -07:00
|
|
|
str(outdir / 'layers.rendered.pdf'), str(outdir / 'pdfa.ps'), context
|
2019-01-04 13:20:41 -08:00
|
|
|
)
|
|
|
|
|
2021-04-07 02:11:31 -07:00
|
|
|
contents = (outdir / 'pdfa.pdf').read_bytes()
|
|
|
|
# Since the XML may be invalid, we scan instead of actually feeding it
|
|
|
|
# to a parser.
|
2022-06-11 01:15:30 -07:00
|
|
|
|
2021-04-07 02:11:31 -07:00
|
|
|
xmp_start = contents.find(XMP_MAGIC)
|
|
|
|
xmp_end = contents.rfind(b'<?xpacket end', xmp_start)
|
|
|
|
assert 0 < xmp_start < xmp_end
|
|
|
|
# Ensure we did not carry the nul forward.
|
|
|
|
assert contents.find(b'�', xmp_start, xmp_end) == -1, "found escaped nul"
|
|
|
|
assert contents.find(b'\x00', xmp_start, xmp_end) == -1
|