How to Extract Transactions from a PDF Bank Statement
A PDF looks like a table but stores positioned text fragments with no rows, columns or reading order. Extracting transactions means rebuilding that structure. Here's how the process works under the hood, and what it means for the accuracy of your data.
Step 1: Get the text out
PDFs downloaded from online banking embed a text layer that can be read precisely, with exact characters and positions. Scanned statements are images — extraction must fall back to OCR, which introduces a recognition step and therefore a margin of error.
Good pipelines detect which kind of document they're holding and only OCR when necessary.
Step 2: Reconstruct the table
With text and positions in hand, the extractor infers column boundaries from alignment, groups fragments into rows, joins wrapped description lines to their parent row, and propagates dates that print once per group. Reliability depends on how clearly the layout is printed, so good extractors validate the result instead of assuming it.
Step 3: Normalize and validate
Raw strings become typed data: dates to ISO 8601, amounts to signed decimals, paired money-in/money-out columns to debit and credit fields, currency detected from symbols and context.
The strongest correctness check is the running balance: each balance should equal the previous balance plus the transaction amount. A chain that reconciles end-to-end is strong evidence that no row was missed or misread.
- ISO dates eliminate locale ambiguity
- Signed amounts allow direct summation
- Balance validation catches missing or corrupted rows
- Confidence scores mark rows needing review
Under the hood: the parser, step by step
In this engine the steps run in a fixed order. pdf.js yields text fragments with x/y coordinates. Fragments within roughly half a line height of each other vertically are one row; a gap larger than about 0.6 of the line height starts a new cell. The header row defines columns, and the first match for each role wins so a stray word later in the page cannot redefine a column.
Each row is then classified: a date plus amounts is a transaction; a date with only a balance is the opening balance; no date but amounts means another transaction on the same day; no date and no amounts is a wrapped description line that belongs to the row above; and lines such as "Total deposits" or "Closing balance" are skipped. Statements split into sections (Deposits, Withdrawals, Checks) take their debit/credit direction from the heading, and tables like "Daily ending balance" are ignored.
Finally the balance chain is checked: opening balance plus credits minus debits should equal each printed balance. Where one row's direction is wrong, the engine can repair it from the balance evidence; where it cannot, the row is flagged.