How to prepare a PDF for RAG without losing context
A step-by-step workflow to clean, convert, measure, and chunk a PDF before feeding it to a retrieval pipeline.
You have a 90-page operations manual and need to feed it into a RAG pipeline. The naive approach is to dump everything in and hope retrieval figures it out. What actually happens: chunks get ripped mid-sentence, tables lose their headers, and the retrieval step returns fragments that confuse the model more than they help.
Preparing a PDF for RAG is not about raw conversion. It is about preserving the document structure that gives text meaning.
Why raw extraction breaks context
PDF is a visual format. The internal structure stores text as positioned fragments, not as a logical flow. Headers, footers, page numbers, and multi-column layouts all end up jumbled when you extract text naively.
For RAG, that noise does two things: it inflates token counts without adding information, and it pollutes chunk boundaries so your retrieval returns partial or misleading segments.
A practical prep workflow
The sequence that works best for most business documents:
- Strip metadata and hidden content first. Author names, JavaScript actions, embedded attachments, and annotations add noise and can leak into your chunks. Clean the file before extracting text.
- Convert to Markdown. A structured Markdown extraction preserves headings, lists, and paragraph boundaries far better than plain text dumps. If the PDF has scanned pages, use OCR during this step.
- Count tokens to pick a chunk strategy. If the full document is under your embedding model limit (say 8192 tokens for text-embedding-3-large), you may not need chunking at all. If it is over, you need to decide where to cut.
- Chunk with overlap. Split by natural boundaries (headings or paragraphs) and add 32 to 64 tokens of overlap so retrieval does not miss information that spans a boundary.
What to preserve, what to drop
Keep: section titles, list structure, table content (even if flattened into text rows), and any reference identifiers (section numbers, clause IDs).
Drop: page numbers, repeated headers/footers, watermark text, and decorative separators. These inflate token usage without helping retrieval precision.
Tools that handle each step
If you want to run the full pipeline locally without uploading:
- Strip Hidden Content and Remove Metadata for cleanup.
- PDF to Markdown for structured extraction with OCR.
- Token Counter to measure before chunking.
- PDF Chunker for RAG to split into token-budgeted segments with overlap.
All run in the browser. The document stays on your machine through the entire pipeline.