Pdf

Utilities for PDF rendering from HTML using WeasyPrint.

Note that you need to add https://pypi.org/project/weasyprint/ to your dependencies if you want to make use of HTML-to-PDF rendering. This is not included by default as it’s quite heavy and requires OS-level dependencies.

This module exposes the public function render_to_pdf() which renders a template with a context into a PDF document (bytes output). You can use “external” stylesheets in these templates, and they will be resolved through django’s staticfiles machinery by the custom UrlFetcher.

Depends on weasyprint.

maykin_common.pdf.render_template_to_pdf(template_name: str, context: dict[str, object], variant: str | None = 'pdf/ua-1', allowed_protocols: Collection[str] | None = ('http', 'https', 'data'), _urlfetcher_fail_on_errors: bool = False) tuple[str, bytes]

Render a (HTML) template to PDF with the given context.

maykin_common.pdf.render_to_pdf(html: str, variant: str | None = 'pdf/ua-1', allowed_protocols: Collection[str] | None = ('http', 'https', 'data'), _urlfetcher_fail_on_errors: bool = False) tuple[str, bytes]

Render the provided HTML to PDF.

The default variant generates accessible PDFs. Technically it’s still an experimental feature in WeasyPrint, so if it’s causing issues, you can pass variant=None instead.