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. Check for sentences cut at boundaries, tables separated from their headers and fragments without enough context. These are failure modes to inspect, not inevitable outcomes for every PDF.
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
Use this sequence as a starting point, then inspect the extracted text:
- Check privacy and unwanted content. Remove metadata or attachments when sharing requires it. This is separate from text cleanup: removing author metadata does not automatically remove repeated headers or reduce the text extracted by a chunker.
- Inspect extraction before choosing a format.Check reading order, tables and scanned pages. Markdown and OCR are options for a downstream pipeline that accepts their output, not guarantees of correct structure. PDFShore's Chunker reads the selected PDF directly; it does not consume the Markdown tool's output or run OCR.
- Count tokens, then choose a retrieval unit.The embedding model's input limit is a ceiling, not the ideal chunk size. Even a document that fits may need smaller passages for focused retrieval. Use the actual provider's tokenizer for its limit.
- Compare sizes and overlap. Start with page mode and compare 512 / 0, 512 / 64 and 1024 / 64. Inspect boundaries and actual token counts before embedding. Overlap can carry a sentence across a boundary, but also repeats text and does not guarantee better retrieval.
Three public PDFs, measured before and after chunking
Tested November 24, 2026 with PDFShore's Token Counter and Chunker source functions, PDF.js 5.7.284 and gpt-tokenizer 3.4.0. We ran the functions in Node with module/worker paths adapted, not a different splitting algorithm. No cleanup, OCR, embedding API or retrieval test. All PDFs were English, with selectable text. Counts below use cl100k_base, the Chunker's encoding, not the Counter's default o200k row.
| Document | Pages | Document tokens | 512 / 0: chunks | 512 / 64: chunks | 1024 / 64: chunks |
|---|---|---|---|---|---|
| UN UDHR (EN) | 8 | 2,189 | 7 | 8 | 3 |
| Attention Is All You Need | 15 | 10,667 | 28 | 29 | 13 |
| NIST AI RMF 1.0 | 48 | 21,619 | 60 | 63 | 36 |
Overlap repeats text; it does not reduce the total text to embed. NIST's exported chunks recounted to 21,460 tokens without overlap and 24,020 with 64-token overlap at the 512 target. These totals need not equal the original 21,619: splitting also changes whitespace and token boundaries. The UI reported 24,028 for the overlap run, eight more than the export recount.
The target is not a guaranteed ceiling. Recounting each exported chunk at 512 / 64 found maxima of 374 tokens (UN), 520 (Attention) and 574 (NIST). Two Attention chunks and ten NIST chunks exceeded 512. At 1024 / 64, Attention still reached 1,072. Validate exported text with your embedding provider's tokenizer before ingestion; the displayed count is not an enforced API limit.
Page and paragraph modes produced identical chunk text in these three 512 / 64 tests. The current extractor joins text items with spaces, so original paragraph breaks are often unavailable. Heading mode is a heuristic, not document-layout parsing; page attribution in non-page modes is approximate. Neither mode guarantees intact tables or section boundaries.
A boundary worth inspecting
In the UN document at 512 / 0, the first chunk ended with “shall strive by” and the next began “teaching and education to promote respect”. A page boundary split a sentence even though the document was short. At 512 / 64, chunk 2 included text from page 1 but its exported page_start/page_end remained 2/2. Keep the original PDF and verify citations, especially where overlap crosses pages.
Start by reviewing the first, last and a table-heavy chunk. Check sentence continuity, identifiers and source pages. Then compare retrieval on questions with known answers before calling one configuration better. We did not measure retrieval accuracy, model answers or provider bills in this test.
Original PDFs used for this test:
Check your PDF's tokens · Open the PDF Chunker
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.