CPU-Only Invoice OCR: 4 Post-Processing Traps Between Text Lines and Structured Data
Cloud OCR APIs charge per call and require sending invoice data to a third party; this MIT-licensed pipeline runs fully offline on a 4-core CPU and matches Tencent's VatInvoiceOCR field-for-field, so teams that process sensitive or high-volume invoices can cut both cost and data-exposure risk without giving up accuracy.
Recognizing a 73-page VAT invoice PDF on a 4-core CPU, with no cloud API and no GPU, is not an OCR problem in the end. The real work is reconstructing the table layout from OCR text boxes. This project runs RapidOCR (PP-OCRv6 + ONNX Runtime) locally, then applies a set of hard-won heuristics that turn raw text lines into a structured output compatible with Tencent Cloud's VatInvoiceOCR.
The four traps: wrapped product names get split across columns by OCR; quantity and unit price often merge into one number, requiring an amount-based split; multi-page invoices have page subtotals that can be mistaken for the overall total; and artistic-font invoice headers are misread, but a 20-digit full-e-invoice number can correct them. Each solution relies on domain constraints like the arithmetic relation between unit price, quantity, and amount, or the fixed structure of the tax total row.
On the 73-page test set, all pages recognized successfully, the line-item amount and tax totals matched the printed invoice exactly (reconciliation difference 0.00), and a field-by-field comparison with Tencent's cloud API showed 128 identical fields and 0 missing. Total runtime was 275 seconds on 4 cores, about 2–4 seconds per image.
The majority of effort in structured invoice extraction goes into layout reconstruction, not character recognition; OCR supplies text coordinates, and the real cost is mapping them back to rows and columns.
Using arithmetic constraints like unit price × quantity ≈ amount is a pragmatic substitute for training a model, and it works because invoices are designed to be self-consistent for auditing.
A stable identifier (the 20-digit invoice number) can reliably correct an unstable field (artistic-font header), a pattern that generalizes well to other document processing problems where one field is known to be trustworthy.
Matching Tencent Cloud's VatInvoiceOCR field-by-field without the cloud suggests that for this class of structured documents, hand-crafted post-processing can achieve parity with paid API responses.