Sub topic 1
Know your documents: native PDFs, scans and images
Prerequisites
This chapter assumes you can read basic Python, know what JSON is, and have called an LLM API at least once. You also need a rough idea of what a PDF is: a file format that describes how a page looks. You do not need any OCR or PDF library experience.
Why the document type decides your pipeline
Every extraction pipeline starts with the same question: does the file contain real text, or only pictures of text? The answer decides which tools you use, how much each page costs, and how often the output will be wrong.
There are three common cases:
| Type | What is inside | Typical source | Ingestion path |
|---|---|---|---|
| Native-text PDF | Characters with positions and fonts | Exported from Word, accounting software, web pages | Text extraction |
| Scanned PDF | One image per page, no text layer | Office scanner, phone scan app | OCR or a vision model |
| Image file | PNG or JPEG photo or screenshot | Phone camera, screenshots | OCR or a vision model |
A fourth case is common in practice: a scan that has already been run through OCR by the scanner software. It contains a hidden text layer, so it looks native to your code. That layer can be wrong, so treat it with suspicion.
Detecting the type automatically
You cannot tell the type from the file extension, because both native and scanned files end in .pdf. Instead, try text extraction and check how much comes back. The pypdf library reads the text layer of each page. A page with almost no text is probably an image.
from pypdf import PdfReader
reader = PdfReader("invoice.pdf")
pages = [page.extract_text() or "" for page in reader.pages]
text = "\n\n".join(pages)
if len(text.strip()) < 50:
print("Very little text found: the PDF is probably scanned and needs OCR.")
else:
print(text[:500])The threshold of 50 characters is a heuristic. It works for a whole short document, but a long file can mix both kinds of pages, such as a native contract with a scanned signature page. For mixed files, apply the check per page, as in the diagram below.
Common errors
- Trusting an empty result. If
extract_text()returns an empty string and you send that to the model, it may invent plausible values. Always check for near-empty text first. - Judging by the whole file. One scanned page in a 40-page native PDF is easy to miss. Check each page.
- Assuming a text layer is correct. Hidden OCR layers can contain misread digits, such as
0read asO. Compare a sample against the rendered page. - Ignoring password protection or damaged files. These raise errors in the reader. Catch them and report the file instead of crashing the batch.
Later chapters cover each path in detail: text extraction, OCR, layout and tables, and sending page images to vision-capable models.
Exercise
A 12-page PDF returns 3,000 characters from extract_text(), but all of that text comes from pages 1 to 10. Pages 11 and 12 return empty strings. What is the likely cause, and what should your code do?
Show answer
Pages 11 and 12 are probably scanned images with no text layer, for example appended signed pages. A whole-file check would pass because the total text is large. The code should check each page's text length and send the near-empty pages to OCR or a vision model, then combine the results in page order.