Public invoice rendering helpers.
Classes
InvoicePDFExporter
Import: from ksef2.renderers import InvoicePDFExporter
class InvoicePDFExporterInvoicePDFExporter(
stylesheet_path: str | Path | None = None,
enable_code_lookups: bool = False,
html_overrides: str | None = None,
*,
page_size: PDFPageSize | None = None,
verification_environment: Environment = Environment.PRODUCTION,
)Export FA3 invoice XML to PDF through XSLT and WeasyPrint.
Methods
InvoicePDFExporter.stylesheet_path (property)
stylesheet_path: PathReturn the path to the XSL stylesheet being used.
Returns
Path— The path to the XSL stylesheet being used.
InvoicePDFExporter.export_from_path
def export_from_path(
invoice_xml_path: str | Path,
*,
ksef_number: str | None = None,
) -> bytesRender an invoice XML file to PDF bytes.
Parameters
invoice_xml_path(str | Path) — Path of the invoice XML file.ksef_number(str | None) — Assigned KSeF number to display below a verification QR.Noneomits the QR. Supports accepted invoices only, not offline certificate QR signing.
Returns
bytes— The PDF as bytes.
Raises
FileNotFoundError— Ifinvoice_xml_pathdoes not exist.KSeFInvoiceRenderingError— If HTML rendering fails.RuntimeError— If WeasyPrint does not return PDF bytes.ValueError— If QR output is requested with an empty number or invalid FA(3) XML.
InvoicePDFExporter.export_from_string
def export_from_string(
invoice_xml: str | bytes,
*,
ksef_number: str | None = None,
) -> bytesRender invoice XML content to PDF bytes.
Parameters
invoice_xml(str | bytes) — Original invoice XML content. Strings are encoded as UTF-8; use bytes to preserve the hash of the submitted document.ksef_number(str | None) — Assigned KSeF number to display below a verification QR.Noneomits the QR. Supports accepted invoices only, not offline certificate QR signing.
Returns
bytes— The PDF as bytes.
Raises
KSeFInvoiceRenderingError— If HTML rendering fails.RuntimeError— If WeasyPrint does not return PDF bytes.ValueError— If QR output is requested with an empty number or invalid FA(3) XML.
InvoicePDFExporter.export_to_file
def export_to_file(
invoice_xml_path: str | Path,
output_pdf_path: str | Path,
*,
ksef_number: str | None = None,
) -> PathRender an invoice XML file and write PDF output to disk.
Parameters
invoice_xml_path(str | Path) — Path of the invoice XML file.ksef_number(str | None) — Assigned KSeF number to display below a verification QR.Noneomits the QR. Supports accepted invoices only, not offline certificate QR signing.output_pdf_path(str | Path) — Path to write the PDF to.
Returns
Path— The path of the written PDF file.
Raises
FileNotFoundError— Ifinvoice_xml_pathdoes not exist.KSeFInvoiceRenderingError— If HTML rendering fails.RuntimeError— If WeasyPrint does not return PDF bytes.ValueError— If QR output is requested with an empty number or invalid FA(3) XML.OSError— If the PDF output file cannot be written.
InvoiceXSLTRenderer
Import: from ksef2.renderers import InvoiceXSLTRenderer
class InvoiceXSLTRendererInvoiceXSLTRenderer(
stylesheet_path: str | Path | None = None,
enable_code_lookups: bool = False,
)Render FA3 invoice XML to HTML using the bundled XSLT stylesheet.
By default, XSLT document() reads cannot access local files or the
network. Enabling code lookups allows read-only file and network access so
the bundled stylesheet can resolve schema documents for code descriptions.
Use enabled code lookups only with trusted stylesheets.
Methods
InvoiceXSLTRenderer.stylesheet_path (property)
stylesheet_path: PathReturn the stylesheet used for rendering.
Returns
Path— Path of the XSLT stylesheet.
InvoiceXSLTRenderer.render_from_path
def render_from_path(invoice_xml_path: str | Path) -> strRender an invoice XML file to HTML.
Parameters
invoice_xml_path(str | Path) — Path of the invoice XML file.
Returns
str— The rendered HTML.
Raises
FileNotFoundError— Ifinvoice_xml_pathdoes not exist.KSeFInvoiceRenderingError— If the stylesheet or invoice XML cannot be parsed, compiled, transformed, or serialized.
Example
from ksef2.renderers import InvoiceXSLTRenderer
html = InvoiceXSLTRenderer().render_from_path("invoice.xml")InvoiceXSLTRenderer.render_from_string
def render_from_string(invoice_xml: str | bytes) -> strRender invoice XML content to HTML.
Parameters
invoice_xml(str | bytes) — Invoice XML content.
Returns
str— The rendered HTML.
Raises
KSeFInvoiceRenderingError— If the stylesheet or invoice XML cannot be parsed, compiled, transformed, or serialized.
InvoiceXSLTRenderer.render_to_file
def render_to_file(invoice_xml_path: str | Path, output_html_path: str | Path) -> PathRender an invoice XML file and write HTML output to disk.
Parameters
invoice_xml_path(str | Path) — Path of the invoice XML file.output_html_path(str | Path) — Path to write the HTML to.
Returns
Path— The path of the written HTML file.
Raises
FileNotFoundError— Ifinvoice_xml_pathdoes not exist.KSeFInvoiceRenderingError— If rendering fails or the output file cannot be written.
Enumerations
PDFPageSize
Import: from ksef2.renderers import PDFPageSize
class PDFPageSize(StrEnum)Supported PDF paper sizes and orientations.
Values
| Name | Value |
|---|---|
A4 | 'A4 portrait' |
A4_LANDSCAPE | 'A4 landscape' |
A3 | 'A3 portrait' |
A3_LANDSCAPE | 'A3 landscape' |
LETTER | 'letter portrait' |
LETTER_LANDSCAPE | 'letter landscape' |
