RAG and search engineeringMiddleLesson 2137 min read

Tables, PDFs and messy documents

Real documents are scanned, multi-column, full of tables and images. Most RAG failures start here, before any search happens.

Lesson in motion

In 60 seconds

Tables, PDFs and messy documents

Real documents are scanned, multi-column, full of tables and images. Most RAG failures start here, before any search happens.

1/5
In simple words
If you photocopy a page crookedly, cut off a corner and mix up the order of pages, even the smartest student cannot answer questions from it. AI is the same. If the text going in is broken, the answers coming out will be broken too.
Many teams spend weeks tuning embeddings and prompts, when the real problem is that the text was extracted badly in the first step. Look at your extracted text before anything else. Open ten random chunks and read them. You will often find the problem in minutes.

Why PDFs are hard

A PDF is designed to look right on a page, not to store text in reading order. Inside, it often holds positioned pieces of text, lines and images. Getting clean text back out is a guess.
ProblemWhat you see after extractionWhat helps
Two or more columnsLines from both columns mixed togetherLayout-aware extraction; check reading order on sample pages
Headers, footers, page numbers"Page 7 of 40 · Confidential" inside every chunkDetect lines repeated on most pages and remove them
Words broken across lines"applic- ation"Rejoin hyphenated line breaks carefully
Special characters and ligatures"file" instead of "file", odd symbolsUnicode normalisation (for example NFKC) and a cleaning step
Indian-script PDFs with older font encodingsGarbled characters that look fine on screenCheck a sample; OCR the rendered page if the text layer is unusable
Scanned pagesNo text at all, only an imageOCR (next section)

Scanned documents and OCR

OCR (optical character recognition) reads text from an image of a page. Government orders, old files, signed letters and photos from phones are often scans. Open-source engines such as Tesseract support many scripts, including several Indian ones, and some vision-language models can also read page images. Quality varies a lot with scan quality, script, fonts and layout.
  1. 1

    Detect

    Check each page: does it have a usable text layer, or is it an image?
  2. 2

    Prepare the image

    Straighten, crop and improve contrast; use a good resolution.
  3. 3

    Run OCR with the right language

    Tell the engine which script and language to expect.
  4. 4

    Check confidence

    Flag pages with low confidence or strange characters for review.
  5. 5

    Store the source

    Keep the page image link, so users can see the original when OCR is doubtful.
Watch out
OCR mistakes are silent. A "5" read as "S" or "₹" dropped from an amount will be searched and quoted as if true. For numbers that matter, such as amounts, dates and ids, show the original page image next to the answer, or verify against a system of record.

Tables

Tables break normal chunking. When a table is flattened into lines of text, the column headers end up far from the numbers, and a chunk may say "12 18 24" with no idea what those mean.
  • Keep a table together when it is small, and store it in a clear text form such as Markdown with the header row.
  • For big tables, repeat the header with every group of rows, or turn each row into a sentence.
  • Add the table's title and unit ("Fees in ₹ per semester") to every table chunk.
  • For numbers people will filter, sum or compare, also load the table into a database. The structured retrieval lesson covers this.
Python: turn table rows into self-contained sentencespython
def rows_to_sentences(title, header, rows):
    """Each row becomes one line that makes sense on its own."""
    out = []
    for row in rows:
        pairs = [f"{col}: {val}" for col, val in zip(header, row)]
        out.append(f"{title} | " + "; ".join(pairs))
    return out

header = ["Course", "Tuition (Rs per semester)", "Hostel (Rs per semester)"]
rows = [["B.Sc. Physics", "18,000", "22,000"], ["B.Com", "15,000", "22,000"]]
for line in rows_to_sentences("Fee schedule 2025-26 (made-up)", header, rows):
    print(line)

Images, charts and diagrams

Text extraction skips images. If a diagram or chart holds important information, you can ask a vision-capable model to write a description or caption, and index that text with a link to the image. Mark such text as a machine description, check a sample by hand, and never treat a description of a chart as exact numbers.
Messy pipeline
  • One extraction setting for every file type
  • Nobody reads the extracted text
  • Tables flattened into lines of numbers
  • OCR output indexed with no quality check
Careful pipeline
  • Different handling for text PDFs, scans, slides and spreadsheets
  • Random samples read by a person after every change
  • Tables kept whole or turned into self-contained rows
  • Low-confidence pages flagged and linked to the original image
Try this. Copy all the text from a two-column PDF (a research paper or newsletter) and paste it into a plain text editor. Is the reading order right? That is what your search index would see.

Watch and read more

Lab

An extraction quality report on three of your own documents, with fixes for the worst problems.

~30 min

The problem

Pick three documents you are allowed to use: one normal text PDF, one with a table, and one scan or photo of a printed page (for example a public notice). Do not use private documents of other people.

  1. Extract the text with any tool you have: copy-paste, a free PDF library, or a free OCR tool.
  2. For each document, list every problem you see: order, headers, broken words, characters, tables, missing text.
  3. Rewrite one table using the rows_to_sentences idea from the lesson.
  4. For each problem, write the fix you would build, and how you would detect the problem automatically across many files.

You are done when

Hard questions

Try to answer before you reveal. If you can answer these, you understood the lesson.

Q1How could you detect page headers and footers automatically across a 300-page PDF?Reveal

Extract the first and last few lines of every page. Normalise them by removing digits (so "Page 7" and "Page 8" look the same) and extra spaces. Lines that appear on most pages are almost certainly headers or footers, so remove them before chunking. Check a sample by eye, because a real heading might repeat on a few pages too.

Q2A scanned Marathi notice is OCRed with many errors. Keyword search on the OCR text fails, but vector search sometimes works. Why, and what would you do?Reveal

Keyword search needs exact word matches, and OCR errors break words, so it fails. Vector search can still catch the general meaning from the words that survived. Improve OCR first: better scans, the correct language setting, image clean-up, or a different engine or vision model. Flag low-confidence pages, and always show the original image as the source so users can read the real text.

Please sign in to continue.

Questions people ask

Which PDF library should I use?

There are many open-source options, and they differ in speed, layout handling and table support. Try two or three on a sample of your documents and read the output. Tools change often, so check current docs and licences.

Should I always OCR, to be safe?

No. If a PDF has a good text layer, extracting it is usually more accurate than OCR. OCR is for scans, and for pages whose text layer is broken, such as some older Indian-script PDFs.

How do I handle Word files, slides and spreadsheets?

Use a reader made for each format, so you keep headings, slide titles and cell structure. Spreadsheets are often better loaded into a database than turned into text.

How much time should go into document processing?

Often more than people expect. For many real projects, cleaning and structuring documents is the single biggest factor in answer quality. Budget for it from the start.

Lesson test

5 questions. Get 3 right (60%) to pass and complete this lesson.

Sign in to take the test and save your progress