Extract PDF text while preserving page references
In this article (5 sections)
Flattening a PDF into one string makes later citations difficult to verify. Preserve the source digest and physical page number with every extracted segment before cleaning or chunking changes the text.
Execute a page-aware extraction
The RAG ingestion lab generates a three-page PDF with ReportLab and extracts it with pypdf.
from ingestion_cases import pdf_pages_case
result = pdf_pages_case()
assert result["page_count"] == 3
assert result["page_numbers_preserved"] is True
assert result["text_matches"] is True
assert result["pages"][1]["page"] == 2Page 2 contains the receipt or verified-order-ID rule. The result also records the PDF SHA-256 digest. Because the PDF is created in a temporary directory from invented text, the check is reproducible and contains no external document.
Preserve extraction provenance
For each page, store document ID/version, content digest, physical page index, printed page label if different, extraction tool/version and text. Keep bounding boxes when layout or evidence highlighting needs them. Headers and footers may need removal, but retain rules and the original page mapping.
A PDF is a presentation format, so reading order can be ambiguous. Multi-column text, footnotes, rotated pages, forms and embedded fonts need explicit tests. Compare extracted samples with the rendered document; text presence alone does not prove correct order.
Carry page identity through chunks
A chunk may span two pages. Store both the start/end page and the smaller page-level source spans that support it. When the assistant cites a statement, the application should link to the exact approved source version and page—not a regenerated page number from a different file.
Scanned image pages require OCR and a separate quality flag. Do not silently mix empty extraction with a genuine blank page. Route uncertain pages to review or abstention for citation-dependent tasks.
The Generative & Agentic AI course connects page-aware extraction to source-grounded answers and citation evaluation.
Exercise
Create a permitted four-page PDF containing two columns, a footer and one scanned page. Extract source spans, verify reading order by hand and show that a cited sentence opens the correct page/version.
Continue learning
This article is part of the RAG ingestion and document preparation sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Review the prerequisite or neighbouring task in Build a RAG document inventory with source ownership.
- Continue with Handle scanned documents with OCR quality checks.
Reference: pypdf text-extraction documentation.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Generative & Agentic AI programme — 3 months. Add practical GenAI, retrieval and agent-building skills to your existing toolkit.
Explore Generative & Agentic AI