Sub topic 11
Deciding between text extraction, page images and native PDF
This chapter assumes you can call a chat API with an SDK, build content blocks, and base64-encode files as shown in earlier chapters. It also assumes you know that images and pages consume tokens from the context window.
The three routes
A PDF can reach a model in three ways.
- Text extraction. A library reads the text layer of the PDF. You send plain text.
- Page images. You render each page to a PNG and send the images, as in the previous chapter.
- Native PDF. You send the file as a
documentblock. The provider reads the text and also looks at each page visually.
Comparing the routes
| Route | Token cost | Keeps layout and charts | Works on scans | Main risk |
|---|---|---|---|---|
| Text extraction | Lowest | No | No | Scrambled order in columns and tables |
| Page images | High, grows with pages | Yes | Yes | Small text misread at low resolution |
| Native PDF | Highest, text plus images | Yes | Yes | Provider page and size limits |
Check for a text layer first
A scanned PDF often contains only pictures of pages. Extraction then returns an empty string. You can test for this before choosing a route.
import fitz # PyMuPDF
def text_layer_chars(pdf_path):
"""Return the number of text characters on each page."""
with fitz.open(pdf_path) as doc:
return [len(page.get_text().strip()) for page in doc]
counts = text_layer_chars("report.pdf")
empty_pages = [i for i, n in enumerate(counts) if n < 20]
print(f"{len(counts)} pages, {len(empty_pages)} with little or no text")If most pages are empty, extraction cannot help. Use page images or a native PDF.
Decision rules
- Start with extraction when the document is plain prose with a real text layer. It is cheapest and fastest.
- Move to native PDF when tables, charts, figures or multi-column layout carry meaning, and your provider supports PDF input.
- Use page images when the provider has no PDF input, when you need to send only a few pages, or when you want control over resolution.
- Mix routes. Extract text for the whole document, then send images of only the pages with charts.
A common error
A frequent mistake is to send a native PDF for every document by default. For a 200-page text-only contract, this multiplies cost for no gain. The opposite mistake is extracting text from a slide deck. The result loses the diagrams, so the model answers from fragments.
Run the same five questions through two routes on a sample of your real documents. Compare accuracy and token counts, then pick the cheapest route that answers correctly.
Exercise
A 40-page PDF is a scanned set of invoices with no text layer. Which route can you rule out, and which would you choose if your provider supports PDF input?
Show answer
Rule out text extraction, because a scan has no text layer and returns nothing. Choose native PDF, or page images if you want control over resolution or need only some pages.
Exercise
A 120-page report is mostly prose, but pages 14 and 15 hold charts the questions refer to. How would you combine routes?
Show answer
Extract the text for the whole report, then render pages 14 and 15 as images and send them with the extracted text. This keeps cost low and preserves the charts.