Reading XRechnung and ZUGFeRD invoices in n8n without any external API (open workflow)

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 createdAt is 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.

Nice build, especially rendering a readable PDF from the XRechnung. One addition for the Upload Mail step, since that’s where forwarding usually breaks: besides not accepting XML, DATEV’s help lists PDF and TIF only, at most 20 MB of attachments per mail, and the sender has to be a confirmed address that also appears as the envelope sender. A plain forwarding rule in the mail client often keeps the original sender and gets rejected, so sending from the confirmed address via your own Send Email node is the safer route.

Disclosure: I’m a co-founder of abi group, we run ernsthaft.ai.

Thanks, Anton — that’s exactly the part that bites people later.

The workflow already sends each invoice as its own mail through a Send Email node, from the sender set in Settings, so the forwarding-rule problem shouldn’t come up. But I hadn’t spelled out why anywhere, so I added it to the README now: PDF/TIF only, 20 MB, and the sender has to be the address confirmed in DATEV. One more limit from the same help article that people run into: max. 10 documents per mail. Not an issue here since it’s one invoice per mail, but anyone batching a monthly upload will hit it.

Out of curiosity — do you see a lot of suppliers sending several invoices in one mail, or as a ZIP? That’s the next case I want to handle properly.

1 me gusta

Thanks, and good catch on the 10 documents. I can’t give you a real frequency: our test mailbox was small (21 mails, 7 of them invoices). The case that tripped us up there was a different one: an invoice that only came as a portal link, with no attachment at all, so nothing reached Upload Mail. Might be worth flagging in the same branch where you handle ZIPs and multi-invoice mails.

Took the portal-link case to heart. A mail that arrives without any invoice file now goes to a person, with the links from the body listed, instead of just vanishing — and ZIPs get unpacked, so the multi-invoice mails are covered too.

While I was in there I added the part that probably saves the most money: every invoice is compared with that supplier’s earlier ones, and an IBAN they’ve never used before stops it until someone has called them (on a number they already knew, not the one in the mail). After that there’s an approval step before anything goes to Upload Mail — by mail, WhatsApp or Telegram, two buttons.

WhatsApp had its own catch, by the way. Free text is only delivered inside Meta’s 24-hour window, and the failure doesn’t come back to the node, so without a wait limit the request would just hang. Now accounting gets told after the timeout.

Your Upload Mail details are in the README. Thanks again — that thread made the workflow a lot better than the version I posted.