1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-09-11 20:47:56 +03:00

Add an option to write PDF forms

This commit is contained in:
Guillaume Ayoub 2023-01-17 15:12:32 +01:00
parent 0b1617edc8
commit a626d6d59b
4 changed files with 17 additions and 10 deletions

View File

@ -148,7 +148,8 @@ 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, version=None, custom_metadata=False):
variant=None, version=None, forms=False,
custom_metadata=False):
"""Render the document to a PDF file.
This is a shortcut for calling :meth:`render`, then
@ -186,8 +187,9 @@ class HTML:
:param bytes identifier: A bytestring used as PDF file identifier.
:param str variant: A PDF variant name.
:param str version: A PDF version number.
:param bool custom_metadata: A boolean defining whether custom HTML
metadata should be stored in the generated PDF.
:param bool version: Whether PDF forms have to be included.
:param bool custom_metadata: Whether custom HTML metadata should be
stored in the generated PDF.
:returns:
The PDF as :obj:`bytes` if ``target`` is not provided or
:obj:`None`, otherwise :obj:`None` (the PDF is written to
@ -200,7 +202,7 @@ class HTML:
counter_style, image_cache)
.write_pdf(
target, zoom, attachments, finisher, identifier, variant,
version, custom_metadata))
version, custom_metadata, forms))
class CSS:

View File

@ -144,6 +144,8 @@ def main(argv=None, stdout=None, stdin=None):
parser.add_argument(
'--pdf-variant', choices=VARIANTS, help='PDF variant to generate')
parser.add_argument('--pdf-version', help='PDF version number')
parser.add_argument(
'--pdf-forms', action='store_true', help='Include PDF forms')
parser.add_argument(
'--custom-metadata', action='store_true',
help='include custom HTML meta tags in PDF metadata')
@ -199,6 +201,7 @@ def main(argv=None, stdout=None, stdin=None):
'identifier': args.pdf_identifier,
'variant': args.pdf_variant,
'version': args.pdf_version,
'forms': args.pdf_forms,
'custom_metadata': args.custom_metadata,
}

View File

@ -319,7 +319,7 @@ class Document:
def write_pdf(self, target=None, zoom=1, attachments=None, finisher=None,
identifier=None, variant=None, version=None,
custom_metadata=False):
custom_metadata=False, forms=False):
"""Paint the pages in a PDF file, with metadata.
:type target:
@ -342,6 +342,7 @@ class Document:
:param bytes identifier: A bytestring used as PDF file identifier.
:param str variant: A PDF variant name.
:param str version: A PDF version number.
:param bool version: Whether PDF forms have to be included.
:param bool custom_metadata: A boolean defining whether custom HTML
metadata should be stored in the generated PDF.
:returns:
@ -352,7 +353,7 @@ class Document:
"""
pdf = generate_pdf(
self, target, zoom, attachments, self._optimize_size, identifier,
variant, version, custom_metadata)
variant, version, custom_metadata, forms)
if finisher:
finisher(self, pdf)

View File

@ -101,7 +101,7 @@ def _use_references(pdf, resources, images):
def generate_pdf(document, target, zoom, attachments, optimize_size,
identifier, variant, version, custom_metadata):
identifier, variant, version, custom_metadata, forms):
# 0.75 = 72 PDF point per inch / 96 CSS pixel per inch
scale = zoom * 0.75
@ -174,11 +174,12 @@ def generate_pdf(document, target, zoom, attachments, optimize_size,
pdf_pages.append(pdf_page)
add_links(links_and_anchors, matrix, pdf, pdf_page, pdf_names, mark)
add_inputs(
page.inputs, matrix, pdf, pdf_page, resources, stream,
document.font_config.font_map)
add_annotations(
links_and_anchors[0], matrix, document, pdf, pdf_page, annot_files)
if forms:
add_inputs(
page.inputs, matrix, pdf, pdf_page, resources, stream,
document.font_config.font_map)
page.paint(stream, scale=scale)
# Bleed