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

2026-12-05来源 dev.toEnglish
RAG on Scanned PDFs: Ask a Case File Questions and Get Page Citations in Python

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.

Why a RAG loader sees nothing

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.

Clean before you chunk

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.

Which passages go to the model

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.

Make the model quote, then check the quote

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.

Highlight the quote on the scan

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:

Answer with two checked citations next to page 51 of the scanned 1890 opinion with the quoted words highlighted

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.

Next step

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.

原文:https://www.a8q9.cn/en/article/rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python-db6cf5.html

相关文章