Public invoice rendering helpers.
Klasy
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.
Metody
InvoicePDFExporter.stylesheet_path (właściwość)
stylesheet_path: PathReturn 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,
) -> bytesRender 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.Noneomits the QR. Supports accepted invoices only, not offline certificate QR signing.
Zwraca
bytes— The PDF as bytes.
Zgłasza
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.
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.Noneomits 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,
) -> PathRender 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.Noneomits 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— 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.
Metody
InvoiceXSLTRenderer.stylesheet_path (właściwość)
stylesheet_path: PathReturn 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) -> strRender 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— 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.
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) -> PathRender 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— Ifinvoice_xml_pathdoes 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)Supported PDF paper sizes and orientations.
Wartości
| Nazwa | Wartość |
|---|---|
A4 | 'A4 portrait' |
A4_LANDSCAPE | 'A4 landscape' |
A3 | 'A3 portrait' |
A3_LANDSCAPE | 'A3 landscape' |
LETTER | 'letter portrait' |
LETTER_LANDSCAPE | 'letter landscape' |
