Technical · 2026-10-06 · 6 min

The Hidden Pitfalls of Reading Invoice Amounts: From Naive OCR to German XML Invoices

HerdPrint is a Mac utility designed for organizing and laying out invoices before printing. When the project began, “extracting the invoice total” looked like a trivial weekend task: invoices always have a total section, so simply run OCR, locate the keyword, and copy the numeric value that follows.

In reality, real-world documents are far messier than expected. We reworked this extraction pipeline several times. Here is how the edge cases unfolded, in the order we encountered them.

Starting with Pure Page OCR (and Where It Fell Short)

The first version was entirely naive. It fed the page into the system OCR engine and scanned the text stream for common summary keywords:

  • English: Total, Amount Due
  • Chinese: 合计, 总计, 价税合计
  • Japanese: 合計, 請求金額
  • Korean: 합계금액

For cleanly formatted domestic invoices, this simple strategy worked reasonably well. But as soon as German and Taiwanese invoices entered our test corpus, failures began to compound.

Challenge 1: Formatting Clashes Across Regions and Currencies

Take a value of 1,240 across different markets:

Region / MarketPrinted FormatExtraction Challenge
United States, UK$1,240.00Comma groups thousands; dot is the decimal separator
Germany1.240,00 €Comma and dot swap roles; currency symbol follows the number
Japan¥1,240No fractional currency unit (no decimal places)
Korea₩1,240, 1,240원OCR frequently degrades to W1,240 or 1,240匙
Taiwan, Hong Kong, MacauNT$1,234, HK$1,200, 1,234 元A single character (元) shared across four distinct currencies

1. Decimal Marks Cannot Be Assumed

Our earliest code took an aggressive shortcut: it stripped out every comma and treated the dot as the decimal point. That heuristic mangled the German amount 1.234,56 into 123456, rendering German invoices completely unparseable.

The parser now determines numerical meaning by analyzing separator position and pattern structure: both 1,234.56 and 1.234,56 cleanly normalize to 1234.56, while an isolated 1.234 followed by three digits safely resolves to the integer 1234.

2. Currency Symbol Positioning and Localization

Currency symbol placement varies by locale. German typography places the euro symbol after the number, whereas English places it before. Early builds rigidly formatted everything as €1,240.00, producing an awkward Americanized rendering for German readers. The interface now adapts to the user’s active locale: a German window displays 1.240,00 €, while an English window shows €1,240.00.

3. Disambiguating the “元” Character

The character “元” appears across Mainland China, Taiwan, Hong Kong, and Macau. Defaulting every instance to Chinese Yuan (¥) misclassified Taiwanese receipts as renminbi. Furthermore, since Hong Kong, Macau, and Taiwan all utilize Traditional Chinese script, script type alone cannot resolve the currency.

We addressed this with an evidence-based inference chain:

  1. Explicit Currency Codes First: Search for unambiguous tokens such as NT$, HK$, TWD, or RMB, as well as explicit names like 新臺幣 or 港幣;
  2. Regional Tax Terminology: If no code exists, scan for distinctive regional vocabulary like 統一發票 (Taiwan uniform invoice) or 營業稅;
  3. User Locale: If the page has no regional markers, consult the user’s system locale;
  4. Script Fallbacks: If all prior checks fail, default to ¥ on Simplified Chinese documents and NT$ on Traditional Chinese documents.

This heuristic handles common real-world nuances gracefully. For example, mainland invoices that mention Hong Kong vendors remain correctly categorized under Chinese Yuan.

4. Fragile Currency Symbols Under OCR

In visual recognition, localized currency symbols degrade much faster than alphanumeric codes. On Korean invoices, ₩ often gets misread as W or #, and the unit 원 turns into 匙 or 別.

This happens because the OCR engine chooses language models based on visual script characteristics: if a layout predominantly features Latin characters, nearby Hangul tokens can be misrouted to a Chinese character model. HerdPrint now enforces a strict contextual check: a trailing W or # adjacent to a number is only translated into ₩ if the broader document context is verified as Korean.

The amount column in a German window: amounts printed as 1.240,00 €, with the comma as the decimal mark and the symbol after the number

Challenge 2: Misreading an Amount Is Far Worse Than Failing to Read It

When an app fails to read an invoice entirely, it marks the document as “Pending” and prompts the user to enter the total manually. That causes mild friction, but it is safe. An incorrect extraction, however, risks under-reporting expenses and causing accounting discrepancies.

In our German test batch, running pure OCR against six electronic invoices produced six complete failures:

Sample DocumentTrue Payable AmountVisual OCR Output
ZUGFeRD BASIC commercial invoice235,62 €198 €
ZUGFeRD taxi receipt18,08 €16,90 €
ZUGFeRD credit/correction note−8,79 €0,23 €
XRechnung EN16931 invoice529,87 €473 $
XRechnung credit memo529,87 €473 $
XRechnung property settlement502,63 €15.387,08 €

Investigating these six errors revealed that none of them stemmed from raw OCR character accuracy. All three root causes were structural:

  1. Totals Placed on Final Pages: These documents are programmatic renderings of structured data: header details appear on page one, line items span the middle pages, and the Belegsummen (summary block) sits strictly at the end. One settlement document spanned seven pages, placing Zahlbetrag 502,63 on page seven while our parser stopped at page one.
  2. Greedy Keyword Priority: German invoices feature multiple summary-like lines. Rechnungssumme ohne USt. represents total net without VAT, and Gesamtbetrag der Abschläge indicates deductions. Both terms ranked higher in our keyword dictionary than the actual payable total Zahlbetrag. Under first-match evaluation, the wrong line captured the value every time.
  3. Broken Glyph Segmentation: On certain rendered pages, Zahlbetrag degraded to zohlbetrag. Explicitly adding German to our OCR language list made zero difference: because German and English share the Latin alphabet, the script selection engine remained unchanged.

The Turning Point: German E-Invoices Already Embed Structured XML

Standard German electronic invoices (including Factur-X, ZUGFeRD, and XRechnung) are packaged as PDF/A-3 hybrid files. The visible PDF page is simply a human-readable rendering; embedded directly within the file is an XML attachment containing the complete machine-readable invoice record. Amounts, VAT breakdowns, currency codes, issue dates, and invoice numbers are all stored in standardized schema fields.

The printed German labels correspond one-to-one with standard XML elements:

Printed LabelXML Schema FieldBusiness Meaning
PositionssummeLineTotalAmountSum of all line items
Rechnungssumme ohne USt.TaxBasisTotalAmountTotal taxable amount excluding VAT
SteuerbetragTaxTotalAmountTotal VAT / tax amount
BruttosummeGrandTotalAmountGross invoice total including tax
Erhaltene AnzahlungenTotalPrepaidAmountAmount already prepaid
ZahlbetragDuePayableAmountNet payable balance (the figure needed for reimbursement)

Two non-obvious engineering details emerged while integrating XML parsing:

  • Attachment Filenames Vary Widely: Different accounting engines name their embedded payload arbitrarily: factur-x.xml, ZUGFeRD-invoice.xml, and cii.xml are all common. Rather than filtering by file extension or name, the parser inspects the embedded attachment streams and identifies the invoice by its root XML elements and namespace.
  • Payable Balance vs. Gross Total: Reimbursements require the actual outstanding balance (DuePayableAmount), not the gross total (GrandTotalAmount). When prepayments exist, these two numbers diverge drastically. In one sample, the gross total was 18.310,63 € and the prepayment was 17.808,00 €, leaving an outstanding payable amount of just 502,63 €. Using the gross total would have introduced an enormous error.

The New Pipeline: Embedded XML First, Visual OCR as Fallback

HerdPrint now uses a three-tier extraction architecture:

  1. Embedded Data First: Inspect the file for an embedded XML invoice document. If a valid DuePayableAmount exists, extract it directly and bypass visual OCR entirely.
  2. Fallback to Page OCR: If no structured attachment is present, fall back to the multi-region OCR pipeline.
  3. Manual Confirmation with Memory: If both methods fail to produce a confident result, mark the invoice as “Pending”. When the user manually corrects an amount, HerdPrint remembers the layout pattern for future documents of that type.

We also introduced three transparency enhancements to the user interface:

  • Source Attribution: The amount field explicitly indicates where the value originated, displaying “read from the invoice’s embedded data”.
  • Prepayment Visibility: When partial prepayments are detected, HerdPrint displays both the gross total and the prepaid deduction alongside the payable balance. Without this context, a user might suspect a bug when the final amount is thirty times smaller than the largest number on the printed page.
  • Structured Data First: When a valid embedded invoice supplies the payable amount, HerdPrint uses that declared value instead of an OCR guess from the rendered page.

Dates follow the same hierarchy. The XML field IssueDateTime provides the true invoice issue date, whereas the prominent date on the page is often Fälligkeit (payment due date). Page OCR previously misidentified a due date of 2026-03-15 as the invoice date, even though the issue date was 2026-03-05. The extracted issue date is strictly used to fill missing metadata; it never overrides a folder name customized for a specific travel schedule.

Results Across 117 Real-World Test Invoices

After deploying this dual-track pipeline, we re-ran our regression suite against our corpus of 117 sample invoices:

  • Total extraction errors dropped from 7 to 1;
  • All six German e-invoice samples returned their expected amounts from embedded XML;
  • The single remaining failure was completely unrelated to regional formatting: a Canadian UPS notice contained the explanatory sentence If total value exceeds $1,600 (CDN), which had been misidentified as an invoice total. We resolved this by adding context-filtering rules for conditional clauses.

The amount column in an English window: one amount on each row, and each date group in the sidebar showing its count and total

Details on amount parsing, date extraction, and summary sheets are covered in the HerdPrint Guide. HerdPrint 1.1.1 is available on the App Store, featuring full Traditional Chinese and German localization along with US Letter paper layouts.

HerdPrint app icon

More notes