Invoices to CSV, entirely in the browser: the five walls I hit
说明与用途
作者构建了一个完全在浏览器中运行的 PDF 转 CSV 工具,使用 pdf.js 进行文本提取,使用 tesseract.js 进行 OCR,避免了服务器上传和每份文档的费用。
问题
Need to convert invoices to CSV without uploading PDFs to a server, respecting client privacy and avoiding per-document OCR costs.
结果
The browser‑only solution runs in ~200 ms for text‑layer invoices and ~4 s for scanned invoices, eliminating server costs and privacy concerns.
工作流
- Open PDF with pdf.js to obtain rendered page and text layer
- Count characters in text layer; if >120 use text layer directly
- If text layer insufficient, render page to canvas and run tesseract.js in a worker
- Group text spans into rows using a 6‑point tolerance
- Parse monetary amounts with a custom regex that handles various currency formats
- Export extracted data as CSV or XLSX
证据
pdf.js opens the file and hands back both the rendered page and, if it exists, the text layer with per-item coordinates.
If the text layer has enough characters, I parse those items directly — no OCR, no guessing, ~200ms for a normal invoice.
If it doesn't (a scan, a photo, a fax), I render the page to a canvas and run tesseract.js over it in a worker.
The naive approach is to group spans that share a y. That works until the first invoice where the vendor name and the invoice number sit 3 points apart vertically.
- 记录日期: 2026-11-01T17:44:35.062Z