Topic 1 of 5 · 4 min read
Parsing PDFs, tables and HTML cleanly
Goal: Pick a parser for a given document set and show where a naive text extraction corrupts tables, columns or reading order.
Retrieval can only be as good as the text you extract, so check what the parser actually produced before you tune anything else. A RAG pipeline that indexes garbled text will retrieve garbled text, and the model will answer from it with full confidence.
Check yourself · 1 of 3
A two-column PDF is extracted with a simple text tool and the lines of both columns are mixed together. What is the most likely cause?
Why it matters
A PDF does not store paragraphs. It stores glyphs at coordinates. A simple extractor reads them in file order, which is often not reading order. Two-column pages get interleaved line by line. Tables become a stream of numbers with no headers. Page headers, footers and page numbers land in the middle of sentences. Scanned pages have no text at all. HTML has the opposite problem: the content is there, buried under menus, cookie banners and footers that repeat on every page.
How to do it
- Sort your documents by type. Digital PDFs, scanned PDFs, Office files, HTML pages and images each need a different path.
- Pick a parser per type. Layout-aware parsers such as Docling and Unstructured detect columns, headings, lists and tables and emit them as typed elements or Markdown. Scans need OCR. For pages where layout carries the meaning, a vision-capable model can read the page image and write out the text and tables.
- Keep structure. Convert tables to Markdown or JSON with a header row. Keep heading levels. Keep list items together.
- Strip boilerplate. Remove repeated headers, footers, page numbers, navigation and cookie text, ideally by detecting lines that repeat across pages.
- Look at the output. Open ten parsed documents and read them next to the originals. This takes twenty minutes and finds most problems.
Before and after
Naive extraction of a two-column datasheet:
Operating temperature Storage Acme Router X2 Page 3 of 12
-10 to 45 C -40 to 85 C
The device must be mounted Do not expose the unit to
vertically with the ports direct sunlight for long periods.
facing down.
The columns are interleaved, the page footer sits in the text, and the table lost its labels. A layout-aware parse returns:
| Parameter | Range |
|---|---|
| Operating temperature | -10 to 45 C |
| Storage temperature | -40 to 85 C |
The device must be mounted vertically with the ports facing down.
What goes wrong
- Silent corruption. Nothing errors. The text just looks slightly off, and retrieval quality drops with no clear cause.
- Empty scans. A scanned PDF yields an empty string, and the file is indexed as if it had no content.
- Boilerplate wins. A footer repeated on every page matches many queries and fills the top results with noise.
- One parser for everything. A parser tuned for clean PDFs does badly on slide exports and web pages.
Parsing is slow and costly for large sets, so run it once, store the result, and re-chunk from the stored text rather than re-parsing.
Examples
The scanned contract that indexed as empty
A team indexes 400 PDFs with a text extractor. Retrieval finds nothing for 60 of them. Those files are scans: the extractor returned an empty string and the pipeline raised no error.
Fix: after parsing, count characters per file and flag any file under a threshold. Route flagged files to OCR, then re-index them.
Choosing parsers for a mixed folder
def pick_parser(path, text_chars):
if path.endswith(".html"):
return "html_main_content"
if path.endswith(".pdf") and text_chars < 200:
return "ocr"
if path.endswith((".pptx", ".key")):
return "layout_aware_or_vision"
return "layout_aware"
The routing rule is simple on purpose. The point is that the choice is explicit and testable per document type.
Key points
- Retrieval quality is capped by extraction quality, so read parsed output next to the original before tuning anything else.
- PDFs store glyphs at coordinates, not paragraphs, so naive extraction interleaves columns and drops table headers.
- Choose a parser per document type: layout-aware parsing for digital files, OCR for scans, a vision model for layout-heavy pages.
- Convert tables to Markdown or JSON with a header row so each value keeps its label.
- Remove repeated headers, footers and navigation, or they will match many queries and crowd out real answers.
- Parse once, store the result, and re-chunk from the stored text.
Common mistakes
- Using one text extractor for every file type and never opening the output.
- Treating an empty or very short extraction as a valid document.
- Flattening tables into running text so values lose their column headers.
- Leaving page headers, footers and menus in the text, where they repeat in every chunk.
Check yourself · 2 of 3
After parsing 300 PDFs, retrieval never returns any of 40 specific files, and no error was raised during ingestion. What should you check first?
Check yourself · 3 of 3
Every top result for many different queries ends with the same line: 'Acme Corp Confidential, Page 7 of 12'. What fix addresses the cause?
Practice items, not counted towards your mastery.
Start here
- Docling: document parsing for generative AI (opens in a new tab)ToolFree
An open-source parser that keeps layout, reading order and tables when converting PDFs, slides and HTML for AI pipelines.
- Unstructured documentation (opens in a new tab)DocsFree
Partitioning, element types and chunking options for PDFs, HTML, Office files and images.
- Chunking strategies for LLM applications (Pinecone) (opens in a new tab)ArticleFree
Compares fixed-size, recursive, structure-aware and semantic splitting and how size changes retrieval.