cd /news/artificial-intelligence/rag-on-scanned-pdfs-ask-a-case-file-… · home › topics › artificial-intelligence › article
[ARTICLE · art-182435] src=dev.to ↗ pub= topic=artificial-intelligence verified=true sentiment=↑ positive

RAG on Scanned PDFs: Ask a Case File Questions and Get Page Citations in Python

A developer built a Python RAG pipeline over scanned PDFs that answers plain-English questions about a case file and returns citations naming the file and page, with each quote programmatically verified against its source passage and highlighted on the page image. Tested on the 62-page 1890 Colton v. Stanford opinion plus two image-only documents, the pipeline OCR'd 65 pages in 51 seconds and indexed 214 passages in 19 seconds on a laptop CPU. In a 16-question evaluation, hybrid meaning-plus-keyword retrieval with cleaned OCR text found the right page for 15 of 16 questions while sending no noise passages to the model, versus 10 of 16 for keyword-only search.

recommendation

Combine embedding search with a couple of keyword matches and verify each model quote against its source passage before showing citations, since keyword-only retrieval missed 6 of 16 questions in the source's test.

more in playbook: Retrieval and RAG
by read4 min views1 publishedDec 5, 2026

A litigation team wants to ask its case file questions in plain English. The case file is scanned, and most RAG tutorials start from text that is already there. This guide builds RAG over scanned PDFs in Python: answers that cite the file and page, quotes checked in code, and the quoted words highlighted on the scan.

Everything below comes from a live run on 4 December 2026, with the OCR Wizard API for the OCR. The case file: the 1890 opinion of the Supreme Court of California in Colton v. Stanford (62 scanned pages, public domain), a one page research memo and a two page settlement agreement written for the tutorial.

research-memo-2025-02.pdf       1 page,  0 words in text layer
settlement-agreement-2025.pdf   2 pages, 0 words in text layer

The memo and the agreement are pure images. The Colton PDF has a text layer from its 2008 digitization, but it reads "tlie" for "the", "wdiich" for "which" and holds 105 stray ^ characters. So every page of every file goes through the same OCR, once, cached next to the PDF: 65 pages in 51 seconds, 9 calls of up to 10 pages to /ocr-pdf.

Page 11 as OCR returns it:

COWAN LIBRARY, 1936
F
•
-3-
That, under such circumstances, the compromise agree-
ment should not be set aside, even though the trustee did

A stamp, a stray letter, a bullet, a page number and a word cut by a hyphen. Over the opinion, 356 words are split across lines. The cleaning drops page numbers and stray marks, joins split words, and skips bookplates, title pages and the library due date slip (pages where most letters are capitals).

Then each page is cut into 120 word passages with a 30 word overlap, never across pages, and indexed twice: SQLite FTS5 for keywords and a small embedding model that runs locally. 214 passages, 19 seconds on a laptop CPU.

16 questions with the page that answers each, 8 passages sent to the model:

Method Right page found Noise passages sent
Keyword only, cleaned 10 / 16 0
Meaning only, cleaned 14 / 16 0
Meaning + 2 keyword matches, raw OCR 16 / 16 3
Meaning + 2 keyword matches, cleaned 15 / 16 0

Keyword search misses questions asked in other words. Search by meaning missed "Does the release cover fraud claims?", where "fraud" ends a long list in the clause. Cleaning did not change the hit rate (one question moved with the passage boundaries), but it kept the bookplate, the title page and the library card out of the model's context.

The model must answer from the numbered passages only and copy the exact words it relies on. The code then checks each quote against its passage:

for c in result["citations"]:
    _, file, page, text = hits[c["excerpt"] - 1]
    c.update(file=file, page=page, verified=norm(c["quote"]) in norm(text))

A live answer:

> How much had Mrs. Colton been allowed to draw for her own use, and why did she want to sell?
Mrs. Colton had been allowed to draw about $175,000 for her own use, and she wanted to sell because
she was determined to get out of the enterprises without waiting and taking her chances on creating
a market.
  [colton-v-stanford.pdf p.51] "had been allowed to draw about $175,000 for her own use"
  [colton-v-stanford.pdf p.50] "Mrs. Colton was determined to get out of the enterprises without waiting and taking her chances on creating a market."

A question the file cannot answer returns Not found in the case file.

OCR Wizard returns every word with its box on the page image. Matching the quoted words against that list (joining hyphen-split words) gives the boxes to draw on a render of the page:

The full scripts (OCR with cache, cleaning, hybrid index, quote check, highlighting) and production tips are in the original article: RAG on Scanned PDFs on ai-engine.net.

Get a key on the OCR Wizard API page and run the four scripts on a case file of your own. To make the same PDFs searchable, see the searchable legal PDF tutorial.

── more in #artificial-intelligence 4 stories · sorted by recency
── more on @ocr wizard api 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/rag-on-scanned-pdfs-…] indexed:0 read:4min 2026-12-05 · —