PDF parsing is the process of extracting text, reading order, layout, tables, images and metadata from a PDF file and converting them into structured output such as plain text, Markdown, JSON or HTML. In Document AI, it is usually the first stage of the pipeline, feeding key information extraction, retrieval and document question answering, so its errors propagate into every later step.
PDF parsing is difficult because the format was designed for faithful visual rendering, not for semantic structure. A page typically stores positioned glyphs and drawing instructions, not paragraphs, headings or table cells, and the parser must reconstruct that logical structure. The term PDF data extraction is often used as a synonym.
Why PDF files are hard to parse
A PDF content stream contains operators that place characters at coordinates in a chosen font, draw lines and paint images. Nothing in the base format guarantees that characters appear in reading order, that spaces exist between words or that a visual table is marked as a table.
Several technical details cause most failures. Fonts without a ToUnicode map produce glyph codes that cannot be converted back to characters, ligatures such as “fi” become single glyphs, and justified text may omit real space characters. Tagged PDF, defined in the PDF specification ISO 32000 and required by the accessibility profile PDF/UA, adds a logical structure tree, but most documents in the wild are untagged or tagged incorrectly.
The other fundamental split is between born-digital PDFs, which contain a real text layer, and scanned PDFs, which contain only page images. Scanned files require optical character recognition before any text exists to parse.
Stages of a PDF parsing pipeline
A complete parser runs a sequence of stages from raw objects to logical structure. Simple tools stop after text extraction; document AI systems continue through layout analysis and table recognition. The table outlines the typical stages.
| Stage | What it does | Typical output |
|---|---|---|
| Object decoding | Decompresses streams, resolves fonts and encodings | Glyphs with coordinates |
| Text layer check | Detects whether real text exists or OCR is needed | Route: native or OCR |
| Character grouping | Merges glyphs into words and lines by spacing | Words and text lines |
| Layout analysis | Detects blocks: headings, paragraphs, figures, tables | Labeled regions |
| Reading order detection | Orders blocks across columns and sidebars | Logical text sequence |
| Table structure recognition | Finds rows, columns and merged cells | Cell grid |
| Serialization | Writes the result in a target format | Markdown, JSON or HTML |
Reading order is a frequent hidden error. A two-column scientific paper parsed line by line across the full page width yields interleaved sentences that look plausible but carry no meaning, which then corrupts search indexes and language model answers.
Rule-based, machine learning and vision-language approaches
PDF parsing approaches range from deterministic rules on text coordinates to end-to-end models that read the page image. The choice depends on document variety, accuracy needs and cost per page.
| Approach | How it works | Strengths | Weaknesses |
|---|---|---|---|
| Text-layer extraction | Reads embedded text and positions directly | Fast, exact characters | Weak on layout and tables |
| Heuristic layout rules | Uses gaps, fonts and lines to infer structure | Predictable, cheap | Breaks on unusual layouts |
| Layout detection models | Object detection trained on datasets like DocLayNet | Robust region labeling | Needs GPU, separate OCR |
| OCR-free models | Transformers such as Donut or Nougat read pixels | Handle equations and scans | Can hallucinate text |
| Vision-language models | General multimodal models convert pages to Markdown | Flexible, little setup | Higher cost, less reproducible |
Production systems usually combine methods: use the native text layer where it is trustworthy, apply a layout model for structure and send only difficult regions, such as scanned tables or formulas, to OCR or a vision-language model. Open-source toolkits such as Docling and hosted services such as LlamaParse follow this hybrid pattern.
Parsing tables, forms and special content
Tables are the hardest common element, because borders may be missing, cells may span several rows or columns and a single table may continue across pages. Parsers detect the table region first, then reconstruct the cell grid from ruling lines, text alignment or a learned model.
Other content needs dedicated handling. Interactive form fields (AcroForm) can be read directly as key-value pairs, which is far more reliable than recognizing filled-in text visually. Mathematical expressions need conversion to LaTeX, and headers, footers and page numbers should be removed so they do not break sentences.
Evaluating quality and avoiding common errors
Parsing quality must be measured against a labeled sample of the real documents, not judged by a few visual spot checks. Typical metrics include character error rate for text, reading-order edit distance, and cell-level accuracy or tree edit distance for tables.
The table lists the errors that practitioners encounter most often and the usual remedies.
| Symptom | Likely cause | Typical fix |
|---|---|---|
| Garbled characters | Missing ToUnicode map or custom encoding | OCR the affected pages |
| Words run together | No real space characters in text layer | Insert spaces from glyph gaps |
| Interleaved columns | Naive top-to-bottom reading order | Layout model with column detection |
| Empty output | Scanned page without text layer | Route the page to OCR |
| Broken tables | Borderless or multi-page tables | Dedicated table structure model |
| Invented content | Generative model hallucination | Cross-check against text layer |
Two further precautions matter for retrieval-augmented generation: keep page numbers and bounding boxes with each chunk so answers can be traced back to the source, and never discard low-confidence regions silently, because missing clauses in contracts or rows in financial statements are costly errors.
Put simply
A PDF looks like a neat printed page. Inside, it is closer to a list of instructions for where to put each letter. It does not know what a paragraph or a table is.
PDF parsing is the work of rebuilding the real document from those scattered pieces. The parser finds the words, puts them in the right order and recognizes headings and tables. It is a bit like reassembling a torn page from fragments on a desk.
In practice
A mid-sized accounting firm wants a question answering assistant over about 40,000 PDF files: annual reports, tax rulings and scanned client letters. A first version extracts only the text layer, and testers soon notice wrong answers about revenue figures because two-column pages and borderless tables come out as scrambled text.
The team rebuilds the pipeline. Each page is checked for a usable text layer, scanned pages go to OCR, a layout model labels headings, paragraphs and tables, and tables are converted to Markdown with their page numbers kept. A labeled sample of 300 pages is used to measure reading order and table cell accuracy before and after the change.
The outcome is far fewer answers based on mangled tables, and every answer now cites the page it came from, so auditors can verify figures in seconds instead of searching through the original report.