DEV Community

Jason Guo
Jason Guo

Posted on

Deterministic PDF Bank Statement Extraction and Running-Balance Verification in TypeScript

Converting PDF bank statements into clean spreadsheets or accounting imports is deceptively hard. Unlike invoices or single-page receipts, bank statements span multiple pages, wrap merchant descriptions across two or three lines, mix deposits and withdrawals in shared or split columns, and occasionally arrive as scanned PDFs or vector-outlined fonts.

When building StatementOK, we designed a pure-TypeScript extraction and reconciliation pipeline that runs without Python runtimes or external bank logins. Here is the core architecture that made the output reliable enough for monthly bookkeeping closes.

1. Word-Coordinate Clustering Before Vision Fallback

Most native digital bank statements still contain exact character coordinates inside the PDF content stream. Before touching an LLM or OCR model, we extract text items with their (x, y) bounding boxes, group items into visual rows using a y-coordinate tolerance, and detect column boundaries (Date, Description, Debit/Credit or signed Amount, and Running Balance) from header geometry.

Only when a file is a scanned image, uses vector-outlined glyphs, or fails row reconstruction do we route it to a vision-OCR fallback. That keeps native PDFs fast and deterministic while still handling scanned statements cleanly—a workflow we document on our bank statement to Excel converter page.

2. Row-by-Row Running Balance Reconciliation

The single biggest failure mode in automated PDF-to-spreadsheet tools is a silent sign flip or a dropped transaction line. Instead of treating extraction as finished once rows are parsed, we run a deterministic ledger check across every consecutive pair of rows:

const TOLERANCE = 0.01;
const expectedDelta = Number((curr.balance - prev.balance).toFixed(2));
const isVerified = Math.abs(expectedDelta - curr.amount) <= TOLERANCE;
Enter fullscreen mode Exit fullscreen mode

Any row where balance[n] - balance[n-1] !== amount[n] within $0.01 is flagged immediately in the review table so the user can inspect and edit that exact cell before downloading.

3. Pure-TypeScript Exports (OOXML, CSV, and Web Connect .qbo)

Accounting software is picky about formatting:

  • Excel (.xlsx): Numbers stored as text strings break SUM() and PivotTables. Writing raw OOXML numeric cells (t="n" with #,##0.00;[Red]-#,##0.00) and freezing the header row (ySplit="1") makes the workbook immediately usable.
  • QuickBooks (.qbo): Generic OFX files fail QuickBooks Web Connect validation if they lack <INTU.BID> or deterministic <FITID> transaction IDs. Generating spec-compliant bank statement to QBO files eliminates duplicate-import headaches.

Takeaways

If you are building any financial document parser, treat arithmetic verification as a first-class stage of the pipeline rather than an afterthought. Running-balance math catches OCR hiccups, multi-line description merges, and inverted signs automatically—before bad numbers ever reach Excel or QuickBooks.

Top comments (0)