Skip to main content

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. A slim ERPNext app, later and 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.

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, 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_cii refuses a model whose BT-24 names another profile before it writes, as it does 4 of KoSIT's 86 test invoices.
  • One preceding invoice: CII D16B holds one (BG-3), so write_cii refuses a model with more, as UBL invoices can carry, before it writes.
  • ZUGFeRD PDFs: embed writes the profile EN 16931 only, from a PDF that already is PDF/A.
  • Peppol: the Peppol BIS rules don't run; a Peppol invoice gets CEN's EN 16931 rules and a HAFL-PROFILE warning.
  • 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.
  • Nesting: validate gives HAFL-XSD for a document nested deeper than 2,048 elements, where the XML Schema check stops, though KoSIT's validator checks it; and it refuses an invoice whose empty elements' locations total more than 10,000,000 characters, as their findings would take 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 saxonche has no wheel for musl (Alpine).
  • Large outside input: reading a hostile file near the default max_size takes a lot of memory; pass a smaller max_size for 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.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for haflinger 0.2.0
File Size Uploaded Provenance Actions
haflinger-0.2.0.tar.gz 539.6 kB With provenance Details for haflinger-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for haflinger 0.2.0
File Interpreter ABI Platform Actions
haflinger-0.2.0-py3-none-any.whl Python 3 none any Details for haflinger-0.2.0-py3-none-any.whl

Total release size: 1.2 MB

Release files / haflinger-0.2.0.tar.gz

Download URL haflinger-0.2.0.tar.gz
Size 539.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7c18575ee430c9ef66d7ca6c86bfa42981dbfb7b9aa9424db07a41c510875ebe
BLAKE2b-256 checksum
How to use checksums
b30d6ee01a531b134a8edd78cc9f19bb952b7c15cb94ecc97432c2191a4958af
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 18, 2026.

Transparency log

Release files / haflinger-0.2.0-py3-none-any.whl

Download URL haflinger-0.2.0-py3-none-any.whl
Size 617.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3278aaab8f1a8c2e47b504a76ae46f8f8e2c5d657c19187594b9251cbac6e829
BLAKE2b-256 checksum
How to use checksums
8a408e29905fa5b8042b116ae13e2caa977bb9f1fb7a491605351e73ac28be4a
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 18, 2026.

Transparency log

Security for haflinger 0.2.0

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.

Select from 2 files

Attestation for haflinger-0.2.0.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 18, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page