Haflinger
E-invoicing in Python: ZUGFeRD, XRechnung and Peppol, on one model of the EN 16931 business terms. Built for ERPNext first. Das Zugpferd für ZUGFeRD und XRechnung.
Status: built from its specs: reading CII, UBL and ZUGFeRD PDFs, validating with the official rules, writing CII and ZUGFeRD PDFs (EN 16931); the model holds the document's terms (references, parties, delivery, payment, allowances and charges, totals, VAT breakdown, supporting documents with their attachments) and a line's references, period, allowances and charges, price and item with its attributes: 82 of the 86 invoices of KoSIT's test suite, read and written again as XRechnung 3.0 with at most one preceding invoice, pass the official rules. Released on PyPI, an alpha: while Haflinger is at 0.x, a minor version may change the API (changelog), and the known limits below say what it doesn't do yet.
What it is
- A Python library. One model of the EN 16931 business terms. It reads invoices in CII, UBL and from ZUGFeRD PDFs, validates them with the official rules, and writes CII and ZUGFeRD PDFs. Any Python software can use it; it needs no ERP system to run or to be tested.
- Built on the official artefacts: KoSIT's and CEN's rules run unchanged, and the official test files are the reference (ADR-003).
- ERPNext first. Haflinger Saddle, an ERPNext app in its own repository, maps ERPNext's sales and purchase invoices to and from the model (ADR-001).
What it is not
- Not a Peppol access point: Peppol goes through a certified provider.
- Not affiliated with or endorsed by Frappe Technologies. ERPNext and Frappe are their trademarks.
Installation
From PyPI, with the extra pdf for ZUGFeRD PDFs; Python 3.14 (later versions should work, but aren't tested), tested on Linux on x86 and ARM and on macOS on Apple Silicon:
uv add "haflinger[pdf]" # or: pip install "haflinger[pdf]"
Example
Reading, validating and writing an invoice:
from dataclasses import replace
from pathlib import Path
from haflinger import TARGET_PROFILES, is_valid, read_invoice, validate, write_cii
data = Path("invoice.xml").read_bytes() # CII or UBL
result = read_invoice(data) # the model, and what reading couldn't take in
findings = validate(data) # the XML Schema's and the official rules' verdict, within 60 s
invoice = result.invoice
print(invoice.number, invoice.totals and invoice.totals.amount_due, is_valid(findings))
# CII, handed out only when it passes the XML Schema and the official rules
# of its target profile: plain EN 16931 or XRechnung 3.0
xml = write_cii(replace(invoice, specification_id=TARGET_PROFILES[0]))
is_valid judges the rules that ran: where BT-24 names no supported profile, or none, validate runs CEN's EN 16931 rules alone and adds a HAFL-PROFILE warning. Reading's findings are no verdict, and an InvoiceWriteError is a refusal whatever findings it holds.
Where German invoicing law (§14 UStG) governs an invoice, which the caller decides, check_german_vat(data) reports what §14(4) UStG asks beyond the rules of its profile, with KoSIT's German rules on any profile: the time of supply, and city and post code of seller and buyer. Factur-X MINIMUM and BASIC WL get HAFL-DE-PROFILE instead, as they are no e-invoices under §14(1) UStG. Every finding is meant to be shown, whatever its flag; is_valid isn't for them. Its docstring lists what it doesn't check (ADR-014).
The names haflinger and haflinger.pdf export are the API; every other module is internal (ADR-012).
The official rules run in a worker process that Haflinger starts, within a time limit: timeout= on validate, check_german_vat, write_cii and embed, by default the environment variable HAFLINGER_RULES_TIMEOUT or 60 seconds.
A ZUGFeRD PDF, with the extra pdf: read_invoice reads it directly; validate the invoice embedded in it.
from haflinger.pdf import embed, extract_xml
pdf = Path("invoice.pdf").read_bytes()
findings = validate(extract_xml(pdf))
zugferd = embed(Path("rendered-pdfa.pdf").read_bytes(), xml) # PDF/A in, PDF/A-3 out; EN 16931 only
Known limits
What Haflinger doesn't do yet (backlog):
- Writing targets EN 16931 and XRechnung 3.0, as CII: not XRechnung's extension or CVD, the other Factur-X profiles, or UBL.
write_ciirefuses a model whose BT-24 names another profile before it writes, as it does 8 of KoSIT's 86 test invoices, which name XRechnung's extension or CVD; written as XRechnung 3.0 instead, 4 of them fail the official rules on codes or terms only their own profile allows. - One preceding invoice: CII D16B holds one (BG-3), so
write_ciirefuses a model with more, as UBL invoices can carry, before it writes. - ZUGFeRD PDFs:
embedwrites the profile EN 16931 only, from a PDF that already is PDF/A-1, -2 or -3 and holds no name tree (/Names, such as named destinations or embedded files) and no associated files (/AF), which embedding would drop. - Peppol: the Peppol BIS rules don't run; a Peppol invoice gets CEN's EN 16931 rules and a
HAFL-PROFILEwarning. - The 2017 edition only: the model holds the terms of EN 16931-1:2017, and reading, validation and writing refuse an invoice of another edition, such as 2026, naming it; extensions, such as XRechnung's, stay in the original invoice.
- ZUGFeRD 1.0: reading takes ZUGFeRD 2 and Factur-X, which are CII D16B; a ZUGFeRD 1.0 invoice (
CrossIndustryDocument), as XML or in a PDF, is refused withInvoiceReadErrornaming the format. - Nesting and other limits:
validategivesHAFL-XSDfor a document nested deeper than 2,048 elements, where the XML Schema check stops, though KoSIT's validator checks it; andvalidateandcheck_german_vatrefuse, withInvoiceReadError, an invoice that holds more than 1,000 empty elements or empty elements whose locations total more than 10,000,000 characters, a number of more than 1,000 digits, or a telephone number of more than 100 characters, on which the rules would take minutes or gigabytes. - Platforms: tested with Python 3.14 on Linux on x86 and ARM and on macOS on Apple Silicon, not on Intel Macs; the official rules run in a worker process that needs a POSIX system, so not Windows, and
saxonchehas no wheel for musl (Alpine). - Large outside input: reading a hostile file near the default
max_sizetakes a lot of memory; pass a smallermax_sizefor invoices from outside.
Development
Python 3.14 and uv; details in the development guide.
uv sync --all-extras
uv run pytest
Documentation: docs/README.md.
Scope
Haflinger is maintained for our own use, first for ERPNext. Contributions are welcome (contributing); support isn't promised. Report vulnerabilities privately, as the security policy describes.
Licence
Haflinger's code is Apache-2.0. The package also ships official files under their own terms, unchanged: CEN's EN 16931 rules under EUPL-1.2, SchXslt under MIT, the XML Schemas of UN/CEFACT, OASIS and W3C under the notices in their headers, and two schemas of ETSI's that carry none. THIRD-PARTY-NOTICES lists them with their terms (ADR-009).
Validation runs the official rules with The Saxon XSLT and XQuery Processor from Saxonica Limited (https://www.saxonica.com/): SaxonC-HE, installed as the saxonche package under MPL-2.0. Haflinger requires it and ships none of its files.
Metadata
Release files for haflinger 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| haflinger-0.3.1.tar.gz | 545.8 kB | With provenance | Details for haflinger-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| haflinger-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for haflinger-0.3.1-py3-none-any.whl |
Total release size: 1.2 MB
Release files / haflinger-0.3.1.tar.gz
| Download URL | haflinger-0.3.1.tar.gz |
|---|---|
| Size | 545.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d39decc40df5a802ad0ed990c7b86c06a5634646af271c2487ba16316dd678cc
|
|
BLAKE2b-256 checksum How to use checksums |
b39d4380f841800c0ec5664a4e59ba832928b41fe88770b688905a7611590cb9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logRelease files / haflinger-0.3.1-py3-none-any.whl
| Download URL | haflinger-0.3.1-py3-none-any.whl |
|---|---|
| Size | 623.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a0e1e07b4fe210aa963cbe38fc6ec68b48b84bcf05cb1acfa9b74d19db322884
|
|
BLAKE2b-256 checksum How to use checksums |
f7208229eec17d628fa0258ed1fd2ffc3e417da4b3b430dcf52ef8113abccf64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logSecurity for haflinger 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for haflinger-0.3.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.