Converting PDFs to Clean Markdown for RAG & LLM Context Windows
How heading hierarchy, repeating header removal, hyphen repair, and token-aware chunk overlap determine retrieval quality in RAG pipelines.
Feeding raw PDF text dumps straight into an embedding model or LLM prompt is one of the fastest ways to degrade Retrieval-Augmented Generation (RAG) accuracy. Raw text extraction loses heading structure, repeats running headers on every chunk, splits hyphenated words mid-sentence, and breaks markdown tables. Converting PDFs into clean, structured Markdown before chunking fixes all four failure modes.
Why Markdown Outperforms Raw Plain Text in RAG
| Extraction Artifact | Raw Plain Text | Structured Markdown |
|---|---|---|
| Section Titles | Indistinguishable from body lines | Explicit #, ##, ### hierarchy |
| Repeating Page Headers / Footers | Injected into every ~2 chunks as noise | Detected across page edges and stripped |
| Line-Wrap Hyphens | Breaks tokenization (configu- ration) | Joined back into clean searchable tokens |
| Tabular Data (CSV / JSON) | Unaligned spaces or noisy punctuation | Aligned GitHub-Flavored Markdown tables |
4 Preprocessing Steps Before Generating Embeddings
1. Infer Headings from Font Size Medians
A PDF page does not store HTML heading tags—only positioned text runs with font transforms. By computing the median body font size across the whole document and promoting significantly larger short lines (`>= 1.55x` for H1, `>= 1.28x` for H2, `>= 1.13x` for H3), you recover document hierarchy without calling an expensive vision LLM.
2. Strip Repeating Headers, Footers & Page Numbers
Corporate whitepapers and manuals repeat phrases like "Confidential — Page 14 of 42" at the top or bottom of every page. Stripping lines that repeat across 50%+ of page boundaries keeps those boilerplate tokens out of your vector store.
3. Rejoin Hyphenated Wraps Carefully
Multi-column PDFs frequently hyphenate words at the right margin. Joining trailing hyphens followed by lowercase continuation words repairs search keywords without corrupting intentional hyphenated terms like cloud-init or client-side.
4. Split on Paragraph Boundaries with Token Overlap
Once you have clean Markdown, avoid fixed character slicing that cuts sentences or code blocks in half. Group whole paragraphs up to a target token ceiling—typically 512 to 1,024 tokens—with 80 to 120 tokens of overlap so retrieval queries hitting near a boundary still retain surrounding context.
Privacy Advantage: Local Browser Extraction
Contracts, architecture specs, and internal runbooks should never be uploaded to random free PDF converters. Running PDF.js inside your own browser tab processes the binary locally in memory, outputs Markdown immediately, and exports ready-to-ingest JSON chunks with zero network exposure.