DEV Community

Cover image for I built a PDF table checker that verifies arithmetic in any PDF without a template
Tae Kim
Tae Kim

Posted on • Originally published at compare-lab.xyz

I built a PDF table checker that verifies arithmetic in any PDF without a template

The problem I kept running into: a financial report where a subtotal on page 8 is wrong, but nobody spots it until the numbers get aggregated into a spreadsheet and something doesn't reconcile.

I wanted a tool that could ingest any PDF with tables and tell me which totals don't add up. The constraint: no template, no column mapping UI, no server. Every byte in the browser.

What it does

Upload a PDF. The tool:

  1. Extracts all text items with their x/y coordinates from each page via pdfjs-dist
  2. Clusters items by y-position into rows (within a 4px snap tolerance)
  3. Detects which rows are "total" or "subtotal" rows by keyword matching
  4. For each total row, identifies numeric columns and sums the data rows above it (back to the previous header or total)
  5. Compares computed sum to stated total — flags differences above a 0.1% tolerance

The hard part: coordinate-based table detection

PDF text items don't know they're in a table. The structure is: a stream of (string, x, y, width) tuples. I have to infer table structure purely from spatial relationships.

The approach:

  • Snap y-coordinates to a 4px grid to handle sub-pixel jitter between lines
  • Cluster x-positions of numeric cells to find "column anchors" — positions that consistently have numbers
  • A column anchor that appears in 2+ data rows becomes a verifiable column
  • For each total row, look up the value at each column anchor and compare to the sum above

This breaks on rotated text, multi-column layouts, and cells that span multiple columns. But it catches the common case — section subtotals in financial reports, grand totals in expense sheets.

Stack

  • Next.js 14, static export on Cloudflare Workers
  • pdfjs-dist for text extraction (CDN worker, no bundling)
  • All detection and arithmetic runs in the browser

Freemium

3 PDFs/month tracked in localStorage. Paid plan unlocks unlimited and CSV export ($9/month).

Live at: https://compare-lab.xyz/pdf-table-checker/

If you work with tabular PDFs regularly and there's a document structure this approach would miss, I'd like to know.

Top comments (1)

Collapse
 
bhavin-allinonetools profile image
Bhavin Sheth

Really like the coordinate-based approach. I've worked with PDF parsing before, and the y-position snapping trick solves a lot of those tiny alignment issues that usually break row detection.