Case study
Document IntelligenceStructured data from PDFs
An extraction pipeline for invoices, receipts and purchase orders: the text layer is read from the PDF, turned into typed fields, validated against business rules and returned for review.
- Role
- Product design, backend, extraction pipeline, frontend
- Status
- Live demo
Stack
- Next.js
- TypeScript
- FastAPI
- Python
- pypdfium2
- Pydantic
- PostgreSQL
- OpenRouter
The samples are synthetic documents generated by the demo. Demo Mode makes no AI calls; AI Mode only runs when an OpenRouter key is configured.

Product experience
Screens captured from the demo
Real screenshots of the running demo, not mockups. Open an image to enlarge it.
Click an image to enlarge
Overview
What it does
Document Intelligence turns a PDF into structured data. The text layer is read with PDFium through pypdfium2, the document type is detected, and the fields are extracted into a typed model.
Every result is validated before it is shown: Pydantic enforces types and ranges, and cross-field checks catch totals that do not add up or a line item whose amount disagrees with quantity × unit price.
- PDF upload with size, page and type limits
- Invoice, receipt and purchase order extraction
- Editable result with live validation issues
- JSON and CSV export
- Demo Mode without AI calls, AI Mode through OpenRouter
Pipeline
The extraction path
Five explicit steps. Each one is visible in the demo, and Demo Mode and AI Mode differ only in how the fields are produced.
- 01
PDF intake
The upload is checked by extension, MIME type, magic bytes, size and page count before PDFium opens it.
- 02
Text layer
PDFium extracts the text through pypdfium2. A PDF without a text layer is rejected with a clear message instead of a fake result.
- 03
Type detection
Keywords identify an invoice, receipt or purchase order. Unsupported document types are rejected.
- 04
Field extraction
Demo Mode returns a prepared result for the samples or parses labelled values; AI Mode asks OpenRouter for JSON.
- 05
Validation and review
Pydantic validates types and amounts, cross-field checks add warnings, and the result can be corrected before export.
Modes
Demo Mode and AI Mode
The difference is explicit in the interface, the API response and the stored result. Neither mode pretends to be the other.
Demo Mode
No API key- Prepared results for the three synthetic samples
- Rule-based parser for other labelled PDFs
- No API key and no network call
- Labelled as Demo Mode everywhere
AI Mode
OpenRouter- The extracted text is sent to OpenRouter
- The response must be JSON and is validated with Pydantic
- One retry if the first answer is unusable
- Disabled until OPENROUTER_API_KEY is set
Architecture
How it fits together
A Next.js page calls a FastAPI module through the same-origin /api/* rewrite. The module reads the PDF in memory, extracts fields, validates them with Pydantic and stores only the result for a short window.
Demo visitor
browser
Next.js page
client components
FastAPI module
extract · validate · export
PDFium
pypdfium2 · text layer
OpenRouter
optional · AI Mode
returns JSON + Pydantic
PostgreSQL 16
di_documents
The PDF is never stored. Only the result and a short excerpt are kept, and they expire automatically.
Component map of this repository
Engineering
Technical decisions
The parts that make this useful as an engineering example rather than a static mockup.
- 01
The PDF is not stored
The upload is processed in memory. Only the validated result and a short text excerpt are kept, and rows expire after the retention window.
- 02
Validation is part of the pipeline
Pydantic models check types, ranges and currency codes; cross-field checks report totals and line-item mismatches as issues the user can fix.
- 03
Demo Mode cannot look like AI
Prepared and rule-based results are marked as such in the API response and the interface, and Demo Mode never calls an external service.
- 04
The AI path is bounded
AI Mode sends a single request, validates the JSON, retries once and then fails with a clear error instead of guessing.
Scope
What this demo does not do
The goal is to show the extraction and validation mechanics clearly, not to replace a production document platform.
- No OCR: image-only PDFs are rejected with an explanation.
- Three document types only: invoice, receipt and purchase order.
- Uploaded files are not stored, and results expire after the retention window.
- AI Mode needs your own OpenRouter key; the hosted demo runs in Demo Mode.
Next step
Need document extraction or a validation pipeline?
I build extraction backends that are honest about their output: typed models, explicit validation and results a person can review.