1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-09-17 15:37:34 +03:00

Add option to set PDF version

Related to #1131.
This commit is contained in:
Guillaume Ayoub 2022-05-23 14:28:19 +02:00
parent 457d82818a
commit bff1dd5b22
5 changed files with 30 additions and 11 deletions

View File

@ -419,6 +419,16 @@ def test_pdfa(version, pdf_version):
assert f'part="{version}"'.encode() in stdout
def test_pdf_identifier():
stdout = _run('--pdf-identifier=abc - -', b'test')
assert b'abc' in stdout
def test_pdf_version():
stdout = _run('--pdf-version=1.4 - -', b'test')
assert b'PDF-1.4' in stdout
@assert_no_logs
def test_unicode_filenames(assert_pixels_equal, tmpdir):
"""Test non-ASCII filenames both in Unicode or bytes form."""

View File

@ -148,7 +148,7 @@ class HTML:
attachments=None, finisher=None, presentational_hints=False,
optimize_size=('fonts',), font_config=None,
counter_style=None, image_cache=None, identifier=None,
variant=None):
variant=None, version=None):
"""Render the document to a PDF file.
This is a shortcut for calling :meth:`render`, then
@ -185,6 +185,7 @@ class HTML:
:param dict image_cache: A dictionary used to cache images.
:param bytes identifier: A bytestring used as PDF file identifier.
:param str variant: A PDF variant name.
:param str version: A PDF version number.
:returns:
The PDF as :obj:`bytes` if ``target`` is not provided or
:obj:`None`, otherwise :obj:`None` (the PDF is written to
@ -193,11 +194,11 @@ class HTML:
"""
return (
self.render(
stylesheets, presentational_hints=presentational_hints,
optimize_size=optimize_size, font_config=font_config,
counter_style=counter_style, image_cache=image_cache)
stylesheets, presentational_hints, optimize_size, font_config,
counter_style, image_cache)
.write_pdf(
target, zoom, attachments, finisher, identifier, variant))
target, zoom, attachments, finisher, identifier, variant,
version))
class CSS:

View File

@ -8,8 +8,8 @@ import sys
import pydyf
from . import HTML, LOGGER, __version__
from .text.ffi import pango
from .pdf import VARIANTS
from .text.ffi import pango
class PrintInfo(argparse.Action):
@ -73,6 +73,10 @@ def main(argv=None, stdout=None, stdin=None):
PDF variant to generate (e.g. ``--pdf-variant pdf/a-3b``).
.. option:: --pdf-version <version-number>
PDF version number (default is 1.7).
.. option:: -p, --presentational-hints
Follow `HTML presentational hints
@ -131,6 +135,7 @@ def main(argv=None, stdout=None, stdin=None):
parser.add_argument('--pdf-identifier', help='PDF file identifier')
parser.add_argument('--pdf-variant', choices=VARIANTS,
help='PDF variant to generate')
parser.add_argument('--pdf-version', help='PDF version number')
parser.add_argument('-p', '--presentational-hints', action='store_true',
help='Follow HTML presentational hints.')
parser.add_argument('-O', '--optimize-size', action='append',
@ -180,6 +185,7 @@ def main(argv=None, stdout=None, stdin=None):
'attachments': args.attachment,
'identifier': args.pdf_identifier,
'variant': args.pdf_variant,
'version': args.pdf_version,
}
# Default to logging to stderr.

View File

@ -297,7 +297,7 @@ class Document:
return root
def write_pdf(self, target=None, zoom=1, attachments=None, finisher=None,
identifier=None, variant=None):
identifier=None, variant=None, version=None):
"""Paint the pages in a PDF file, with metadata.
:type target:
@ -319,6 +319,7 @@ class Document:
post-processing on the PDF right before the trailer is written.
:param bytes identifier: A bytestring used as PDF file identifier.
:param str variant: A PDF variant name.
:param str version: A PDF version number.
:returns:
The PDF as :obj:`bytes` if ``target`` is not provided or
:obj:`None`, otherwise :obj:`None` (the PDF is written to
@ -328,7 +329,7 @@ class Document:
pdf = generate_pdf(
self.pages, self.url_fetcher, self.metadata, self.fonts, target,
zoom, attachments, finisher, self._optimize_size, identifier,
variant)
variant, version)
if finisher:
finisher(self, pdf)

View File

@ -11,13 +11,13 @@ import pydyf
from fontTools import subset
from fontTools.ttLib import TTFont, TTLibError, ttFont
from . import pdfa
from .. import Attachment, __version__
from ..html import W3C_DATE_RE
from ..links import make_page_bookmark_tree, resolve_links
from ..logger import LOGGER, PROGRESS_LOGGER
from ..matrix import Matrix
from ..urls import URLFetchingError
from . import pdfa
from .stream import Stream
VARIANTS = {
@ -236,14 +236,15 @@ def _create_bookmarks(bookmarks, pdf, parent=None):
def generate_pdf(pages, url_fetcher, metadata, fonts, target, zoom,
attachments, finisher, optimize_size, identifier, variant):
attachments, finisher, optimize_size, identifier, variant,
version):
# 0.75 = 72 PDF point per inch / 96 CSS pixel per inch
scale = zoom * 0.75
PROGRESS_LOGGER.info('Step 6 - Creating PDF')
pdf = pydyf.PDF()
pdf.version = b'1.7'
pdf.version = str(version or '1.7').encode()
states = pydyf.Dictionary()
x_objects = pydyf.Dictionary()
patterns = pydyf.Dictionary()