Document parsing for RAG: PDFs, tables, and scans

TL;DR
- RAG answers are only as good as the text you feed in, and most of that quality is won or lost at the parsing stage, long before retrieval.
- PDFs, tables, and scans each break naive text extraction in their own way, and a broken parse looks fine in a database while quietly poisoning every answer.
- Build a quality-first pipeline: pick extraction methods per document type, preserve structure, chunk after parsing, and inspect the output before you trust it.
Why RAG quality is decided at the parsing stage
Most teams who are unhappy with their RAG system go looking for a better model, a better embeddings model, or a fancier vector database. We do this work for a living, and the real culprit is almost always further upstream: the documents were parsed badly, so the system is retrieving and reasoning over garbage. A model cannot answer correctly from text that lost its meaning the moment it left the PDF.
Think of parsing as the foundation. Everything downstream (chunking, embedding, retrieval, generation) inherits whatever the parser produced. If your parser drops a table's column headers, merges two newspaper-style columns into one run-on paragraph, or silently skips a scanned page, no amount of clever retrieval will recover the lost information. The answer was never in the index to begin with.
The frustrating part is that bad parsing is invisible by default. The pipeline runs, the vectors get stored, the dashboard goes green. Nothing errors. You only discover the damage when a user asks a question whose answer lived in a mangled table, and the system confidently makes something up. That is why parsing deserves more of your attention than the model choice does.
- Parsing sets the ceiling on quality; nothing downstream can exceed it.
- Bad parses fail silently, so problems surface as wrong answers, not errors.
- Model and database swaps cannot recover information the parser already lost.
- Fixing parsing usually beats every other tuning effort for the effort spent.
Why PDFs are so hard to parse
A PDF is not a document in the way you think of one. It is a set of drawing instructions: put this glyph at these coordinates, draw this line here. There is often no underlying notion of a paragraph, a reading order, or which words belong together. Your parser has to reverse-engineer the structure a human sees instantly, and that reconstruction is where things go wrong.
Multi-column layouts are a classic trap. A two-column page reads top-to-bottom, left column then right. A naive extractor reads by vertical position across the whole page width, so it interleaves the two columns line by line and produces nonsense. Headers, footers, page numbers, and running titles get stitched into the body text, so the phrase 'Confidential - Page 12' ends up embedded mid-sentence inside a chunk.
Then there is everything that is technically text but semantically structure: section numbering, footnotes, sidebars, captions, watermarks. A good parse keeps these in their place and labels them. A bad parse flattens them into one undifferentiated stream, and the hierarchy that told a reader 'this is a subsection of that' is gone for good.
- PDFs store glyph positions, not paragraphs, so reading order must be inferred.
- Multi-column pages get interleaved by position-based extractors into nonsense.
- Headers, footers, and page numbers leak into body text and pollute chunks.
- Footnotes, captions, and sidebars get flattened into the main text stream.
- Section hierarchy is lost when structure is reduced to a flat string.
How naive text extraction destroys tables
Tables are where naive extraction does the most quiet damage. A table's meaning lives in its grid: a number means something only because of the row it sits in and the column above it. Strip the grid away and you are left with a list of numbers and words in roughly reading order, with no way to know which value belongs to which label.
Here is the failure we see constantly. A pricing table with regions down the side and tiers across the top gets extracted as a single line of text: 'Region Tier A Tier B Tier C Americas 40 55 70 EMEA 38 52 66'. Embed that, retrieve it, and ask the model 'what is the Tier B price for EMEA?' It has a coin-flip's chance, because the relationship between 52 and EMEA and Tier B was destroyed at parse time. The data is technically present and completely useless.
The fix is to treat tables as first-class objects. Detect them, extract them as actual rows and columns, and serialise them in a form that preserves the relationships, such as Markdown tables or HTML, or even row-wise sentences like 'For EMEA, Tier B costs 52.' Which form works best depends on your embeddings model and your questions, but anything that keeps cell-to-header links beats a flattened blob every time.
- Table meaning lives in row and column relationships, not the raw values.
- Flattening a table into one text line severs every cell from its labels.
- Detect tables explicitly and extract them as structured rows and columns.
- Serialise to Markdown, HTML, or row-wise sentences that keep header links.
- Test retrieval with real table questions; that is where flattening shows up.
Scanned documents and when you need OCR
Plenty of the documents your business actually relies on are scans: signed contracts, old policy manuals, faxed forms, photographed receipts. To a parser, a scanned PDF is just an image. There is no text layer to extract at all, so a text-based parser will happily return an empty string and move on, and your green dashboard will tell you everything is fine.
This is where optical character recognition (OCR) earns its keep. OCR reads the pixels and produces actual text. The first thing your pipeline needs is detection: figure out whether a page has a real text layer or is just an image, and route image-only pages to OCR automatically. Mixed documents, where some pages are digital and some are scanned, are common and need this routing logic or whole sections vanish.
OCR is not free of error, though. Scan quality, skew, handwriting, faint stamps, and unusual fonts all degrade accuracy, and OCR mistakes propagate straight into your answers. Budget for a quality pass: confidence scoring, deskewing and cleanup before OCR, and spot-checking the output. For high-stakes documents, getting OCR right is the difference between a usable knowledge base and a plausible-looking one.
- Scanned pages have no text layer, so text-only parsers return nothing silently.
- Detect image-only pages and route them to OCR automatically.
- Handle mixed documents where digital and scanned pages are interleaved.
- Pre-process with deskew and cleanup to lift OCR accuracy on poor scans.
- Score confidence and spot-check; OCR errors flow straight into answers.
Handling images, figures, and charts
Documents are not only text and tables. Architecture diagrams, charts, screenshots, and labelled figures often carry the exact information a user will ask about, and a text parser walks right past them. If the answer to 'what was the Q3 trend?' lives only inside a line chart, no text extraction will ever find it.
You have a few honest options here. The simplest is to capture any text the figure contains (axis labels, captions, legends) and index that, which is cheap and partial. A stronger option is to generate a text description of each figure, often with a vision model, and index the description alongside the surrounding text so the chart becomes searchable in plain language. The richest option is to keep the image itself and use a multimodal model at answer time.
You do not need to do this for every document. Most of the time, captions and surrounding context are enough. Reserve figure description for the documents where the visuals genuinely carry meaning the prose does not, because generating descriptions for thousands of decorative images is a cost with no payoff. Match the effort to where the information actually lives.
- Figures and charts can hold the exact fact a user will ask about.
- At minimum, index figure captions, axis labels, and legend text.
- For meaningful visuals, generate a text description and index it too.
- Keep the source image when answer-time multimodal reasoning is worth it.
- Do not describe every decorative image; spend the effort where meaning lives.
Layout-aware vs plain-text extraction, and when to use vision models
There is a spectrum of parsing approaches, and choosing the right point on it per document type is one of the highest-impact decisions in the whole pipeline. At the cheap end is plain-text extraction: pull the text layer, take what you get. It is fast and almost free, and for clean, single-column, text-only documents it is genuinely fine. Do not over-engineer those.
Layout-aware extraction is the middle ground and the right default for most real-world documents. These tools understand the page geometry: they detect columns, reading order, tables, headers, and footers, and they preserve hierarchy. They cost more compute than plain text but produce structured output you can actually trust, and for the messy enterprise PDFs most teams are drowning in, that structure is the whole point.
Vision models (multimodal LLMs that read the page as an image) are the heavy end. They handle the truly ugly cases: complex nested tables, dense forms, mixed scanned-and-digital pages, layouts no rule-based tool can untangle. They are also slower and more expensive per page, so they are not a default; they are a targeted tool. Our rule of thumb: start layout-aware, measure where it fails, and escalate only those document types to a vision model. Paying vision-model prices to parse a clean one-column memo is waste.
- Plain text: fast and cheap, fine for clean single-column documents only.
- Layout-aware: the right default for messy real-world PDFs with structure.
- Vision models: reserve for complex tables, dense forms, and hard scans.
- Match the method to the document type rather than picking one for everything.
- Escalate to vision models on measured failure, not on assumption.
Chunking, metadata, and a quality-first pipeline
Chunking comes after parsing, and that order matters. If you split by a blind character count before you understand the document's structure, you will slice tables in half, cut sentences mid-thought, and separate a heading from the section it introduces. Parse first so you know where the real boundaries are, then chunk along them: by section, by table, by logical unit. Structure-aware chunks retrieve far better than fixed-size windows.
Metadata is the quiet multiplier most teams skip. Every chunk should carry where it came from: source document, page number, section heading, document type, date, and any access tags. Good metadata lets you filter retrieval ('only the current contract version'), cite sources in answers, and debug failures by tracing a bad answer back to the exact chunk and page. A chunk with no provenance is a chunk you cannot trust or fix.
Put it together and the pipeline is simple to state: classify each document by type, route it to the right extraction method, preserve structure and hierarchy, chunk along real boundaries, attach rich metadata, and then (this is the step everyone skips) inspect the parsed output before you index it. Read a sample. Look at the tables. Confirm the scans came through. Spending an hour eyeballing parser output has saved us more grief than any model upgrade. Quality-first means you verify the foundation before you build on it.
- Chunk after parsing so splits land on real structural boundaries.
- Prefer section, table, and logical-unit chunks over blind fixed-size windows.
- Attach source, page, section, type, and date metadata to every chunk.
- Use metadata to filter retrieval, cite sources, and debug bad answers.
- Always inspect a sample of parsed output before trusting the index.
Start here
If you take one thing away, make it this: before you blame the model, look at what your parser is feeding it. Pull ten of your hardest documents (the multi-column reports, the dense tables, the old scans), run them through your current pipeline, and read the raw extracted text. You will usually find the problem in minutes, and it will not be the model.
From there, the path is incremental. Classify your documents by type, set plain-text extraction as the floor and layout-aware as the default, route scans through OCR, and escalate only the genuinely hard cases to a vision model. Get tables extracted as structure, attach metadata to every chunk, and inspect before you index. Do that, and the rest of your RAG system suddenly starts looking a lot smarter, because for the first time it is working from text that actually means what the original document meant.
- Read the raw parser output for your ten hardest documents first.
- Set layout-aware extraction as the default and plain text as the floor.
- Route scans through OCR and escalate only hard cases to vision models.
- Extract tables as structure and attach metadata to every chunk.
- Inspect parsed output before indexing, every time.
Want this built for your business?
We map the highest-leverage place to start and ship a first live system within two weeks.
Book a strategy callCommon questions
Why does my RAG system give wrong answers even with a good model?
Usually because the documents were parsed badly before they ever reached the model. If parsing flattened a table, interleaved columns, or skipped a scanned page, the correct information was never in your index. The model cannot answer from text that lost its meaning at the parsing stage, so fixing parsing tends to help more than swapping models.
What is the best way to extract tables for RAG?
Detect tables as first-class objects and extract them as real rows and columns, then serialise them in a form that keeps each cell linked to its headers, such as a Markdown or HTML table or row-wise sentences. Never let a table be flattened into a single line of text, because that severs every value from its label and makes the data useless for retrieval.
When do I need OCR in my parsing pipeline?
Whenever a page is an image rather than digital text, which is common for signed contracts, old manuals, and photographed forms. Add detection that checks whether each page has a real text layer and routes image-only pages to OCR automatically, since text-only parsers return nothing for scans without erroring. Many real documents mix digital and scanned pages, so this routing is not optional.
Should I use a vision model to parse all my documents?
No. Vision models are slower and more expensive per page, so they are a targeted tool, not a default. Start with layout-aware extraction, measure where it fails, and escalate only the genuinely hard document types (complex tables, dense forms, difficult scans) to a vision model. Using one on clean single-column text is wasted spend.
Should I chunk before or after parsing?
After. If you chunk by blind character count before understanding structure, you slice tables in half and split sections from their headings. Parse first so you know where the real boundaries are, then chunk along them by section, table, or logical unit, and attach metadata like source, page, and heading to every chunk so you can filter, cite, and debug.
Usually because the documents were parsed badly before they ever reached the model. If parsing flattened a table, interleaved columns, or skipped a scanned page, the correct information was never in your index. The model cannot answer from text that lost its meaning at the parsing stage, so fixing parsing tends to help more than swapping models.
Ask AI about X18 Global
“What does X18 Global (x18global.com) do for enterprise AI and automation - and can you summarise their guide "Document parsing for RAG: PDFs, tables, and scans"?”