typstgenv0.6.2

E-invoices (ZUGFeRD, XRechnung)

Turn an invoice into a ZUGFeRD / Factur-X e-invoice — a PDF/A-3 with the EN 16931 invoice data embedded as XML — or into XRechnung / Peppol BIS Billing XML, from the same JSON that renders the invoice.

A ZUGFeRD (Factur-X) invoice is a PDF/A-3 that carries the invoice data a second time, as machine-readable XML (factur-x.xml, UN/CEFACT CII). typstgen builds that XML from the invoice JSON and company.json, the same files the invoice template of the Typst package business-templates renders, so the PDF and the XML cannot drift apart. The profile is EN 16931 (ZUGFeRD “Comfort”).

typstgen compile rechnung.typ --pdf-standard a-3b --creation-timestamp "$(date +%s)" \
  --zugferd rechnung.json --company company.json

rechnung.typ can be any document, for example:

#import "@local/business-templates:0.1.0": invoice
#invoice(data: json("/rechnung.json"), company: json("/company.json"))

typstgen then

  1. checks the data and writes the CII XML,
  2. lets Typst embed it as factur-x.xml with the relationship “Alternative”,
  3. adds the Factur-X schema to the XMP metadata. Typst cannot write custom XMP, so this is appended as an incremental update; Typst’s own output stays byte for byte in the file.

A PDF/A-3 standard (a-3b, a-3u or a-3a) is required.

What is read from the JSON

EN 16931 business-templates JSON
Invoice number, date (BT-1, BT-2) metadata.invoice_number, metadata.invoice_date
Delivery date (BT-72) metadata.delivery_date (optional)
Billing period (BT-73/74) metadata.performance_period as MM/YYYY (optional)
Seller (BG-4) company.json: name, address, contact.email, vat_id and/or tax_id
Buyer (BG-7) recipient: company (or name), address, vat_id; metadata.customer_number as buyer ID
Lines (BG-25) items: description, sub_items, quantity, unit, unit_price, vat_rate, total
Payment (BG-16, BT-20, BT-9) payment.bank_account (or the company’s), payment.payment_terms, payment.due_date

Units are mapped to UN/ECE Recommendation 20 codes (Tage → DAY, Std. → HUR, Stk → H87, Pauschal → LS …); set unit_code on an item for anything else. Countries are given as names (Deutschland) or as country_code (ISO 3166-1).

VAT categories come from vat_rate.code:

code Category Needs
Standard, Reduced (or any rate above 0) S a rate above 0
Zero Z rate 0
Exempt (alias Kleinunternehmer) E rate 0 and exemption_reason (text) or exemption_reason_code
ReverseCharge AE rate 0 and the buyer’s recipient.vat_id
IntraCommunity K rate 0, the buyer’s recipient.vat_id, a delivery date or period and metadata.delivery_country_code; XRechnung also wants delivery_city and delivery_postal_code
Export G rate 0

Checks

Before writing anything typstgen recomputes the invoice: line amounts from quantity × price, VAT per category, totals. If the JSON states a line total or a total that differs, the build fails, because the PDF would show a different number than the XML. All problems are reported at once:

error: ZUGFeRD invoice:
recipient.address.country: unknown country, set recipient.address.country_code (ISO 3166-1, e.g. "DE")
items[0]: unit "Kisten" has no UN/ECE code, set items[0].unit_code (e.g. "C62")
items[1]: total 1.00 does not match quantity × unit price = 11200.00

These checks cover the rules this data can break; they are not a complete EN 16931 validator. Check the result with an external validator before you send invoices, for example Mustang (PDF/A-3 and the XML against EN 16931):

java -jar Mustang-CLI.jar --action validate --source rechnung.pdf

Invoices built from the business-templates examples, with mixed VAT rates, reverse charge and an exempt invoice, were validated with Mustang 2.26.0 and veraPDF 1.30 on 10 October 2026 without errors. Without delivery_date Mustang reports a warning (PEPPOL-EN16931-R008, empty delivery element); the invoice stays valid.

From an AI agent

The MCP tool compile takes the same input:

{ "path": "rechnung.typ", "pdf_standards": ["a-3b"],
  "zugferd": { "invoice": "rechnung.json", "company": "company.json" } }

XRechnung and Peppol BIS Billing

typstgen e-invoice writes the same invoice as a pure-XML e-invoice in UBL 2.1. The XML comes from the same checked invoice model as the CII XML above, never from a rendered PDF, so amounts, tax groups, parties and dates agree.

typstgen e-invoice rechnung.json --company company.json --format xrechnung-3.0
typstgen e-invoice rechnung.json --company company.json --format peppol-bis-3.0 -o rechnung.peppol.xml
--format Syntax Specification identifier (BT-24)
xrechnung-3.0 UBL 2.1 urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0
peppol-bis-3.0 UBL 2.1 urn:cen.eu:en16931:2017#compliant#urn:fdc:peppol.eu:2017:poacc:billing:3.0

Both use the business process urn:fdc:peppol.eu:2017:poacc:billing:01:1.0 (BT-23). Any other format name is an error; there is no fallback to a PDF.

Besides the fields in the table above, the formats read:

EN 16931 business-templates JSON XRechnung 3.0 Peppol BIS 3.0
Seller electronic address (BT-34) company.json: electronic_address {scheme_id, value} required required
Buyer electronic address (BT-49) recipient.electronic_address {scheme_id, value} required required
Buyer reference (BT-10) metadata.buyer_reference, e.g. the Leitweg-ID required this or BT-13
Order reference (BT-13) metadata.order_reference optional this or BT-10
Seller contact (BG-6) company.json: contact.name, contact.phone, contact.email all required optional
Payment means (BT-81) payment.method_code (UNTDID 4461); default 58 with a bank account required optional

scheme_id is a code from the EAS list, for example 0204 (Leitweg-ID), 9930 (German VAT ID), 0088 (GLN) or EM (e-mail). A credit transfer (30, 58) needs an IBAN. Missing fields are reported with their JSON path:

error: ZUGFeRD invoice:
company.electronic_address: required ({"scheme_id": "0204", "value": "…"})
metadata.buyer_reference: required (the Leitweg-ID for public buyers)
company.contact.phone: required by XRechnung (seller contact)

After writing the file, typstgen prints which checks ran:

checks: arithmetic passed, required_fields passed, xml_schema not_run, en16931 not_run, xrechnung not_run, transmission not_run

The UBL schema and the Schematron rules (EN 16931, XRechnung, Peppol) are not run by typstgen and are never reported as passed. Check the XML with the KoSIT validator and its XRechnung configuration, or with the Peppol validation artefacts, before sending it. Writing the file is not a transmission: sending it over the Peppol network needs an access point, and public-sector portals have their own upload.

The MCP tool e_invoice takes invoice, company, format and an optional output and returns the same check states as JSON.

Not covered

  • XRechnung and Peppol BIS Billing in the CII syntax, and other national formats.
  • Credit notes (type code 381), allowances and charges, prepaid amounts.
  • Other currencies than the one in the JSON are not converted.

Edit this page on GitHub · Docs for v0.6.2