Sub topic 10
Rendering PDF pages as images
This chapter assumes you can send a base64 image to an LLM API, as covered earlier, and that you know basic Python file handling. Native PDF input is not always available or affordable. In those cases you can turn each page into a PNG and send the pages as ordinary images.
Why render pages yourself
Rendering gives you control that native PDF input does not.
- Any provider with vision support can read the pages, even if it has no PDF block.
- You choose which pages to send, so you pay only for the pages you need.
- You choose the resolution, which sets the balance between legibility and token cost.
- You can crop or downscale each page with the same tools you use for photos.
The cost is that the model sees only pixels. It does not receive the PDF's embedded text layer, so small print depends entirely on your rendering quality.
Rendering with PyMuPDF
PyMuPDF (imported as fitz) draws each page into a pixmap, which is a raster image held in memory. The dpi argument sets the resolution in dots per inch.
import fitz # PyMuPDF
def render_pages(pdf_path, dpi=150):
"""Render each PDF page to PNG bytes."""
pages = []
with fitz.open(pdf_path) as doc:
for page in doc:
pix = page.get_pixmap(dpi=dpi)
pages.append(pix.tobytes("png"))
return pages
pngs = render_pages("report.pdf")
print(f"Rendered {len(pngs)} pages")Install it with pip install pymupdf. The function returns a list of PNG byte strings, one per page, in page order.
Choosing a DPI
A US Letter page is 8.5 by 11 inches. At 150 DPI that is about 1275 by 1650 pixels, which fits within common image size limits. At 300 DPI it is 2550 by 3300 pixels. Most providers downscale images that large, so the extra pixels add upload size without adding detail.
| DPI | Approx. size (Letter) | Typical use |
|---|---|---|
| 100 | 850 x 1100 | Large text, slides |
| 150 | 1275 x 1650 | Default for reports |
| 200 | 1700 x 2200 | Dense tables, footnotes |
Start at 150. Raise the value only if the model misreads small text, and check your provider's documentation for its maximum image dimensions.
Sending the pages
Each PNG becomes one image block. Encode the bytes as base64 and put the question after the images.
import base64
import anthropic
client = anthropic.Anthropic()
pngs = render_pages("report.pdf")
content = []
for png in pngs[:3]: # only the first three pages
data = base64.standard_b64encode(png).decode("utf-8")
content.append(
{
"type": "image",
"source": {"type": "base64", "media_type": "image/png", "data": data},
}
)
content.append({"type": "text", "text": "Summarize these pages."})
message = client.messages.create(
model="MODEL_NAME", # replace with a vision-capable model
max_tokens=1024,
messages=[{"role": "user", "content": content}],
)
print(message.content[0].text)Common mistakes
- Skipping page selection. A 200-page PDF becomes 200 images. Filter by page range before rendering.
- Using too high a DPI. Large images are downscaled by the provider anyway, so you pay in upload time for nothing.
- Losing page order. Keep the list index as the page number, and say in the prompt which page each image is.
Exercise
A 40-page PDF has its financial table on pages 12 and 13, and the model misreads some small footnote digits at 150 DPI. What two changes would you make to the pipeline?
Show answer
Render only pages 12 and 13 instead of all 40, which keeps cost and request size down. Then raise the DPI for those pages, for example to 200, so the footnote digits have more pixels, while staying within the provider's maximum image dimensions.