{"slug": "rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python", "title": "RAG on Scanned PDFs: Ask a Case File Questions and Get Page Citations in Python", "summary": "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.", "body_md": "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.\n\nEverything below comes from a live run on 4 December 2026, with the [OCR Wizard API](https://ai-engine.net/apis/ocr-wizard?utm_source=devto&utm_medium=referral&utm_campaign=crosspost) 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.\n\n```\nresearch-memo-2025-02.pdf       1 page,  0 words in text layer\nsettlement-agreement-2025.pdf   2 pages, 0 words in text layer\n```\n\nThe 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`.\n\nPage 11 as OCR returns it:\n\n```\nCOWAN LIBRARY, 1936\nF\n•\n-3-\nThat, under such circumstances, the compromise agree-\nment should not be set aside, even though the trustee did\n```\n\nA 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).\n\nThen 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.\n\n16 questions with the page that answers each, 8 passages sent to the model:\n\n| Method | Right page found | Noise passages sent | \n|---|---|---|\n| Keyword only, cleaned | 10 / 16 | 0 | \n| Meaning only, cleaned | 14 / 16 | 0 | \n| Meaning + 2 keyword matches, raw OCR | 16 / 16 | 3 | \n| **Meaning + 2 keyword matches, cleaned** | **15 / 16** | **0** | \n\nKeyword 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.\n\nThe 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:\n\n```\nfor c in result[\"citations\"]:\n    _, file, page, text = hits[c[\"excerpt\"] - 1]\n    c.update(file=file, page=page, verified=norm(c[\"quote\"]) in norm(text))\n```\n\nA live answer:\n\n```\n> How much had Mrs. Colton been allowed to draw for her own use, and why did she want to sell?\nMrs. Colton had been allowed to draw about $175,000 for her own use, and she wanted to sell because\nshe was determined to get out of the enterprises without waiting and taking her chances on creating\na market.\n  [colton-v-stanford.pdf p.51] \"had been allowed to draw about $175,000 for her own use\"\n  [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.\"\n```\n\nA question the file cannot answer returns `Not found in the case file`.\n\nOCR 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:\n\nThe 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](https://ai-engine.net/blog/rag-scanned-pdf-ocr-python?utm_source=devto&utm_medium=referral&utm_campaign=crosspost).\n\nGet a key on the [OCR Wizard API page](https://ai-engine.net/apis/ocr-wizard?utm_source=devto&utm_medium=referral&utm_campaign=crosspost) 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://ai-engine.net/blog/searchable-pdf-legal-documents-python?utm_source=devto&utm_medium=referral&utm_campaign=crosspost).", "url": "https://wpnews.pro/news/rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python", "canonical_source": "https://dev.to/aiengine/rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python-4ffc", "published_at": "2026-12-05 14:37:24+00:00", "updated_at": "2026-12-05 14:43:30.156562+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-tools", "natural-language-processing"], "entities": ["OCR Wizard API", "ai-engine.net", "Colton v. Stanford", "Supreme Court of California", "SQLite FTS5"], "also_reported_by": [], "takeaway": "Hybrid retrieval combining local embeddings with two keyword matches per query hit the correct page on 15 of 16 questions with zero noise passages, while keyword-only search managed 10 of 16 and meaning-only 14 of 16.", "recommendation": {"advice": "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.", "playbook": "rag-retrieval", "playbook_url": "https://wpnews.pro/playbooks/rag-retrieval"}, "alternates": {"html": "https://wpnews.pro/news/rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python", "markdown": "https://wpnews.pro/news/rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python.md", "text": "https://wpnews.pro/news/rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python.txt", "jsonld": "https://wpnews.pro/news/rag-on-scanned-pdfs-ask-a-case-file-questions-and-get-page-citations-in-python.jsonld"}}