Skip to content


Repository files navigation

drafthorse -- Basic ZUGFeRD implementation in Python

This is a low-level python implementation of the ZUGFeRD XML format. ZUGFeRD is a German format for sending digital invoices. ZUGFeRD XML files are to be attached to a PDF file. This library can be used to generate or parse the contents of this XML file as well as attach it to a PDF. We do not support parsing PDF files (for now).

By low-level, we mean that this library tries to model the ZUGFeRD data model 1:1 without any further abstractions or simplifications. You can set and parse all parameters defined in ZUGFeRD 2.2.

All output is validated against the official XSDs, but no validation of profile levels (basic, comfort, extended) is performed.



from drafthorse.models.document import Document
samplexml = open("sample.xml", "rb").read()
doc = Document.parse(samplexml)


import os
from datetime import date
from decimal import Decimal

from drafthorse.models.accounting import ApplicableTradeTax
from drafthorse.models.document import Document
from drafthorse.models.note import IncludedNote
from drafthorse.models.tradelines import LineItem
from drafthorse.pdf import attach_xml

# Build data structure
doc = Document() = "" = "RE1337"
doc.header.type_code = "380" = "RECHNUNG"
doc.header.issue_date_time =

note = IncludedNote()
note.content.add("Test Node 1")
doc.header.notes.add(note) = "Lieferant GmbH" = "Kunde GmbH" = "Kunde GmbH" = "Kunde GmbH" = "EUR" = "ZZZ"

li = LineItem()
li.document.line_id = "1" = "Rainbow"
li.agreement.gross.amount = Decimal("999.00")
li.agreement.gross.basis_quantity = (Decimal("1.0000"), "C62")  # C62 == pieces = Decimal("999.00") = (Decimal("999.00"), "EUR") = (Decimal("1.0000"), "C62")  # C62 == pieces
li.settlement.trade_tax.type_code = "VAT"
li.settlement.trade_tax.category_code = "E"
li.settlement.trade_tax.rate_applicable_percent = Decimal("0.00")
li.settlement.monetary_summation.total_amount = Decimal("999.00")

trade_tax = ApplicableTradeTax()
trade_tax.calculated_amount = Decimal("0.00")
trade_tax.basis_amount = Decimal("999.00")
trade_tax.type_code = "VAT"
trade_tax.category_code = "AE"
trade_tax.exemption_reason_code = 'VATEX-EU-AE'
trade_tax.rate_applicable_percent = Decimal("0.00") = Decimal("999.00") = Decimal("0.00") = Decimal("0.00") = Decimal("999.00") = Decimal("0.00") = Decimal("999.00") = Decimal("999.00")

# Generate XML file
xml = doc.serialize(schema="FACTUR-X_EXTENDED")

# Attach XML to an existing PDF.
# Note that the existing PDF should be compliant to PDF/A-3!
# You can validate this here:
with open("input.pdf", "rb") as original_file:
    new_pdf_bytes = attach_xml(, xml, 'EXTENDED')

with open("output.pdf", "wb") as f:


To run the included tests:

pip install -r requirements_dev.txt
py.test tests

To automatically sort your Imports as required by CI:

pip install isort
isort -rc .

To validate files using mustang:

git clone
cd mustangproject
git checkout core-2.5.1
./mvnw clean package
java -jar Mustang-CLI/target/Mustang-CLI-2.5.1-SNAPSHOT.jar --action validate --source invoice.pdf

Credits and License

Maintainer: Raphael Michel <>

License of the Python code: Apache License 2.0

The PDF handling (drafthorse/ is based on the code of factur-x, Copyright 2016-2018, Alexis de Lattre <>, released under a BSD license.

The packages includes schemas and samples of the ZUGFeRD specification (.xsd and .xml files) which are owned by the Forum für elektronische Rechnungen bei der AWV e.V („FeRD“) and are released under a proprietary license that allows bundling them together with other software for free.