Since January 2025 every company in Germany has to be able to receive e-invoices. In n8n two things make that annoying: an XRechnung is plain XML, and ZUGFeRD / Factur-X hides the XML as an attachment inside a PDF/A-3. Most guides solve the second part with a paid API. Here is how it works inside a Code node — maybe it saves someone a few hours.
1. Getting the XML out of a ZUGFeRD PDF
The XML sits in an embedded file stream, usually Flate-compressed. The stream dictionary contains /Type /EmbeddedFile, so you read the binary, find those streams and inflate them. require('zlib') is blocked in the Code node (and on Cloud you can’t add modules), so I wrote a small inflate (RFC 1951, ~80 lines) in plain JS. The search part looks like this:
const buf = await this.helpers.getBinaryDataBuffer(0, 'data');
const text = buf.toString('latin1');
const re = /<<((?:(?!>>\s*stream)[\s\S]){0,2000})>>\s*stream\r?\n/g;
for (let m; (m = re.exec(text)); ) {
if (!/\/Type\s*\/EmbeddedFile\b/.test(m[1])) continue;
const start = m.index + m[0].length;
const raw = buf.subarray(start, text.indexOf('endstream', start));
// zlib stream = 2-byte header + raw deflate
const xml = (/\/FlateDecode/.test(m[1]) ? inflateRaw(raw.subarray(2)) : raw).toString('utf8');
}
This found the XML in all 150 PDFs of the public ZUGFeRD corpus (ZUGFeRD 1.0, 2.x, Factur-X).
2. One parser for CII and UBL
Same data, different element names. If you drop the namespace prefixes and allow alternatives per path step (ApplicableHeaderTradeSettlement|ApplicableSupplyChainTradeSettlement), one small parser covers ZUGFeRD 1.0, 2.x, XRechnung CII and UBL. All 72 invoices of the KoSIT test suite parse cleanly.
3. Things I learned the hard way
- DATEV Upload Mail does not accept XML at all, only document images. If bookkeeping goes through DATEV, you need a readable PDF of the XRechnung — that can also be rendered in plain JS with the standard Helvetica fonts.
- A lot of official sample invoices use made-up IBANs. An IBAN mod-97 check flags them instantly, which is just as useful on real invoices.
- Data Table insert with automatic mapping fails as soon as the item has a field that isn’t a column. And
createdAtis a reserved column name.
Plain PDFs without any XML go to a local Ollama model in my setup and are always marked for manual review — I wouldn’t trust an LLM with invoice amounts unchecked.
If you want the complete workflow: it’s on GitHub under DenisShokhirev041279 / n8n-e-rechnung. Happy to answer questions about the PDF part, that took the longest.