GitHub

ksef2.renderers

Public invoice rendering helpers.

Na tej stronie

Public invoice rendering helpers.

Klasy

InvoicePDFExporter

Import: from ksef2.renderers import InvoicePDFExporter

class InvoicePDFExporter
python
InvoicePDFExporter(
    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,
)
python

Export FA3 invoice XML to PDF through XSLT and WeasyPrint.

Metody

InvoicePDFExporter.stylesheet_path (właściwość)

stylesheet_path: Path
python

Return the path to the XSL stylesheet being used.

Zwraca

  • 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,
) -> bytes
python

Render an invoice XML file to PDF bytes.

Parametry

  • invoice_xml_path (str | Path) — Path of the invoice XML file.
  • ksef_number (str | None) — Assigned KSeF number to display below a verification QR. None omits the QR. Supports accepted invoices only, not offline certificate QR signing.

Zwraca

  • bytes — The PDF as bytes.

Zgłasza

  • FileNotFoundError — If invoice_xml_path does 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,
) -> bytes
python

Render invoice XML content to PDF bytes.

Parametry

  • 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. None omits the QR. Supports accepted invoices only, not offline certificate QR signing.

Zwraca

  • bytes — The PDF as bytes.

Zgłasza

  • 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,
) -> Path
python

Render an invoice XML file and write PDF output to disk.

Parametry

  • invoice_xml_path (str | Path) — Path of the invoice XML file.
  • ksef_number (str | None) — Assigned KSeF number to display below a verification QR. None omits the QR. Supports accepted invoices only, not offline certificate QR signing.
  • output_pdf_path (str | Path) — Path to write the PDF to.

Zwraca

  • Path — The path of the written PDF file.

Zgłasza

  • FileNotFoundError — If invoice_xml_path does 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 InvoiceXSLTRenderer
python
InvoiceXSLTRenderer(
    stylesheet_path: str | Path | None = None,
    enable_code_lookups: bool = False,
)
python

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.

Metody

InvoiceXSLTRenderer.stylesheet_path (właściwość)

stylesheet_path: Path
python

Return the stylesheet used for rendering.

Zwraca

  • Path — Path of the XSLT stylesheet.

InvoiceXSLTRenderer.render_from_path

def render_from_path(invoice_xml_path: str | Path) -> str
python

Render an invoice XML file to HTML.

Parametry

  • invoice_xml_path (str | Path) — Path of the invoice XML file.

Zwraca

  • str — The rendered HTML.

Zgłasza

  • FileNotFoundError — If invoice_xml_path does 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")
python

InvoiceXSLTRenderer.render_from_string

def render_from_string(invoice_xml: str | bytes) -> str
python

Render invoice XML content to HTML.

Parametry

  • invoice_xml (str | bytes) — Invoice XML content.

Zwraca

  • str — The rendered HTML.

Zgłasza

  • 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) -> Path
python

Render an invoice XML file and write HTML output to disk.

Parametry

  • invoice_xml_path (str | Path) — Path of the invoice XML file.
  • output_html_path (str | Path) — Path to write the HTML to.

Zwraca

  • Path — The path of the written HTML file.

Zgłasza

  • FileNotFoundError — If invoice_xml_path does not exist.
  • KSeFInvoiceRenderingError — If rendering fails or the output file cannot be written.

Typy wyliczeniowe

PDFPageSize

Import: from ksef2.renderers import PDFPageSize

class PDFPageSize(StrEnum)
python

Supported PDF paper sizes and orientations.

Wartości

NazwaWartość
A4'A4 portrait'
A4_LANDSCAPE'A4 landscape'
A3'A3 portrait'
A3_LANDSCAPE'A3 landscape'
LETTER'letter portrait'
LETTER_LANDSCAPE'letter landscape'