PDF parsing

Also known as: PDF data extraction

On this page 7

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.

StageWhat it doesTypical output
Object decodingDecompresses streams, resolves fonts and encodingsGlyphs with coordinates
Text layer checkDetects whether real text exists or OCR is neededRoute: native or OCR
Character groupingMerges glyphs into words and lines by spacingWords and text lines
Layout analysisDetects blocks: headings, paragraphs, figures, tablesLabeled regions
Reading order detectionOrders blocks across columns and sidebarsLogical text sequence
Table structure recognitionFinds rows, columns and merged cellsCell grid
SerializationWrites the result in a target formatMarkdown, JSON or HTML
Typical stages of PDF parsing

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.

ApproachHow it worksStrengthsWeaknesses
Text-layer extractionReads embedded text and positions directlyFast, exact charactersWeak on layout and tables
Heuristic layout rulesUses gaps, fonts and lines to infer structurePredictable, cheapBreaks on unusual layouts
Layout detection modelsObject detection trained on datasets like DocLayNetRobust region labelingNeeds GPU, separate OCR
OCR-free modelsTransformers such as Donut or Nougat read pixelsHandle equations and scansCan hallucinate text
Vision-language modelsGeneral multimodal models convert pages to MarkdownFlexible, little setupHigher cost, less reproducible
Comparison of PDF parsing approaches

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.

SymptomLikely causeTypical fix
Garbled charactersMissing ToUnicode map or custom encodingOCR the affected pages
Words run togetherNo real space characters in text layerInsert spaces from glyph gaps
Interleaved columnsNaive top-to-bottom reading orderLayout model with column detection
Empty outputScanned page without text layerRoute the page to OCR
Broken tablesBorderless or multi-page tablesDedicated table structure model
Invented contentGenerative model hallucinationCross-check against text layer
Common PDF parsing errors and fixes

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.