Skip to content

Commit

Permalink
Set default PDF variant values in options before generating PDF
Browse files Browse the repository at this point in the history
We then have the PDF identifier and version set to the variant defaults during
generation.
  • Loading branch information
liZe committed Jun 7, 2024
1 parent d772c83 commit 61f8bb3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ def test_pdfa_compressed(version, pdf_version):
_run(f'--pdf-variant=pdf/a-{version}b - -', b'test')


def test_pdfa1b_cidset():
stdout = _run('--pdf-variant=pdf/a-1b --uncompressed-pdf - -', b'test')
assert b'PDF-1.4' in stdout
assert b'CIDSet' in stdout


def test_pdfua():
stdout = _run('--pdf-variant=pdf/ua-1 --uncompressed-pdf - -', b'test')
assert b'part="1"' in stdout
Expand Down
22 changes: 11 additions & 11 deletions weasyprint/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,24 +388,24 @@ def write_pdf(self, target=None, zoom=1, finisher=None, **options):
new_options = DEFAULT_OPTIONS.copy()
new_options.update(options)
options = new_options
pdf = generate_pdf(self, target, zoom, **options)

identifier = options['pdf_identifier']
compress = not options['uncompressed_pdf']
version = options['pdf_version']
variant = options['pdf_variant']

# Set default PDF version for PDF variants.
if version is None and variant:
if variant := options['pdf_variant']:
_, properties = VARIANTS[variant]
if 'version' in properties:
version = properties['version']
if 'identifier' in properties and not identifier:
identifier = properties['identifier']
if 'version' in properties and not options['pdf_version']:
options['pdf_version'] = properties['version']
if 'identifier' in properties and not options['pdf_identifier']:
options['pdf_identifier'] = properties['identifier']

pdf = generate_pdf(self, target, zoom, **options)

if finisher:
finisher(self, pdf)

identifier = options['pdf_identifier']
compress = not options['uncompressed_pdf']
version = options['pdf_version']

if target is None:
output = io.BytesIO()
pdf.write(output, version, identifier, compress)
Expand Down

0 comments on commit 61f8bb3

Please sign in to comment.