Document processing is the unsexy foundation of AI applications. An LLM can't reason about a document it can't read. Yet most teams underinvest here, leading to RAG systems that fail on anything beyond clean text files.
The Document Processing Pipeline
Every document pipeline has the same stages:
PDF Processing: The Hard Part
PDFs are where document processing dreams go to die. They're designed for printing, not parsing.
PDF Types
Native PDF
Created digitally. Text is extractable. Use PyMuPDF or pdfplumber.
Scanned PDF
Images of paper. Requires OCR. Quality varies wildly.
Mixed PDF
Some pages native, some scanned. Need to detect and handle both.
Protected PDF
Copy-protected or encrypted. May need special handling or be blocked.
PDF Extraction Tools
- • PyMuPDF (fitz) — Fast, reliable for native PDFs
- • pdfplumber — Better for tables, slower overall
- • Azure Document Intelligence — Best accuracy, handles scans, tables, structure
- • AWS Textract — Good for forms and tables
- • Unstructured.io — Open-source, handles many formats
OCR for Scanned Documents
When text isn't extractable, OCR (Optical Character Recognition) converts images to text.
OCR Options
- • Tesseract — Open-source, free, good for clean scans
- • Google Cloud Vision — High accuracy, handles noise well
- • Azure Computer Vision — Excellent with handwriting
- • AWS Textract — Good for structured forms
OCR Quality Tips
- • Preprocess images — Deskew, denoise, increase contrast
- • Set correct DPI — 300 DPI minimum for OCR
- • Specify language — Improves accuracy significantly
- • Post-process text — Spell-check, fix common OCR errors
Table Extraction
Tables are information-dense and poorly handled by most extraction tools. Specialized handling is essential.
- • Camelot — Python library, excellent for native PDF tables
- • Tabula — Java-based, good accuracy
- • Azure Document Intelligence — Best for complex/spanning tables
- • Vision models — GPT-4V, Claude can extract tables from images
Tip: Convert extracted tables to Markdown or JSON for LLM consumption. Raw cell data loses structure.
Normalization Layer
Raw extracted text is messy. Normalization creates consistent, clean output:
- • Unicode normalization — Fix encoding issues, weird characters
- • Whitespace cleanup — Remove excessive newlines, normalize spacing
- • Header/footer removal — Strip repeated page elements
- • Reading order — Reorder multi-column text correctly
- • Section detection — Identify headings, paragraphs, lists
Metadata Enrichment
Extract metadata for filtering and context:
- • Document type — Contract, invoice, report, email
- • Date/timestamps — Document date, processing date
- • Author/source — Who created it, where it came from
- • Language — Detect and tag language
- • Named entities — Companies, people, locations mentioned
Pipeline Architecture
Production pipelines need to be resilient and observable:
Quality Monitoring
Track extraction quality over time:
- • Extraction coverage — % of documents successfully processed
- • Character confidence — OCR confidence scores
- • Table detection rate — Are tables being found?
- • Error patterns — Which document types fail most?
- • Human sampling — Periodically review extraction quality
Common Pitfalls
Assuming all PDFs are the same
Test with real customer documents early. Demos work on clean files; production breaks on scanned faxes.
Ignoring table structure
Flattened table text is often useless. Preserve row/column relationships.
No fallback strategy
When extraction fails, have a plan. Queue for manual review? Use vision model?
Need a document processing pipeline?
We build production-grade document pipelines that handle the messy reality of enterprise documents.
Contact me