pdf-reading
PDF Processing Guide
Overview
This guide covers essential PDF reading operations using Python libraries and command-line tools. For advanced features (pypdfium2 rendering, pdfplumber table settings, OCR fallback, encrypted/corrupted PDF handling), see REFERENCE.md.
Reading & Inspecting PDFs
Before doing anything with a PDF, understand what you're working with.
Content inventory
Run a quick diagnostic first. For simple tasks ("summarize this
document"), pdfinfo + pdffonts + a text sample may suffice. For
anything involving figures, attachments, or extraction issues, run the
full set:
# Always: page count, file size, PDF version, metadata
pdfinfo document.pdf
# Always: does a text layer exist? No fonts → scanned/raster → see "Scanned documents"
pdffonts document.pdf
# If fonts are present: sample the text layer
pdftotext -f 1 -l 1 document.pdf - | head -20
# If figures/charts may matter:
pdfimages -list document.pdf
# If the PDF might contain embedded files (reports, portfolios):
pdfdetach -list document.pdfThis tells you:
- Page count and size — how big is the job?
- Font status — are any fonts present? An empty
pdffontstable
means the PDF is scanned or raster-only: pdftotext will return
nothing, so skip straight to "Scanned documents" below. Fonts shown
as not embedded ("emb: no") with custom encodings may produce wrong
characters on extraction.
- Text extractability — when fonts exist, does
pdftotextreturn
clean text, or is it garbled (broken encoding)?
- Embedded raster images — are there photos or raster figures?
(Note: vector-drawn charts from matplotlib/Excel won't appear — see
"Extracting embedded images" below)
- Attachments — are there embedded spreadsheets, data files, etc.?
Text extraction
pypdf for basic text:
from pypdf import PdfReader
reader = PdfReader("document.pdf")
print(f"Pages: {len(reader.pages)}")
# Extract text
text = ""
for page in reader.pages:
text += page.extract_text()pdftotext preserving layout (better for multi-column docs):
# Layout mode preserves spatial positioning
pdftotext -layout document.pdf output.txt
# Specific page range
pdftotext -f 1 -l 5 document.pdf output.txtpdfplumber for layout-aware extraction with positioning data:
import pdfplumber
with pdfplumber.open("document.pdf") as pdf:
for page in pdf.pages:
text = page.extract_text()
print(text)Visual inspection (rasterize pages)
Text extraction is blind to charts, diagrams, figures, equations,
multi-column layout, and form structures. When any of these matter,
rasterize the relevant page and Read the image:
# Rasterize a single page (page 3 here) at 150 DPI
pdftoppm -jpeg -r 150 -f 3 -l 3 document.pdf /tmp/page
# pdftoppm zero-pads the output filename based on TOTAL page count
# (e.g., page-03.jpg for a 50-page PDF, page-003.jpg for 200+ pages)
# Don't guess the filename — find it:
ls /tmp/page-*.jpgThen Read the resulting image file. This gives you full visual
understanding of that page — layout, charts, equations, everything.
When to rasterize vs. text-extract:
- Content/data questions → text extraction (cheaper, searchable)
- Figures, charts, visual layout → rasterize the page
- Tables → try text extraction first, rasterize if garbled
- Precision matters → do both (extract text AND rasterize; use text
for data, image for context — this is what Claude's API does natively
with PDF uploads)
Token cost awareness:
- Text extraction: ~200–400 tokens per page
- Rasterized image: ~1,600 tokens per page (at 150 DPI)
- Both together: ~2,000–2,400 tokens per page
For a 100-page PDF, rasterizing everything would consume ~160K tokens.
Only rasterize pages that matter for the question at hand.
Choosing your reading strategy
Text-heavy documents (reports, articles, books):
→ Text extraction is primary. Rasterize only for specific figures or
pages where layout matters.
Scanned documents (pdffonts shows no fonts):
→ pdftotext will return nothing — don't run it. Rasterize pages at
150 DPI and Read them visually. For bulk text extraction, use OCR
(pytesseract after converting pages to images — see REFERENCE.md for
a complete example).
Slide-deck PDFs (exported presentations):
→ Every page is primarily visual. Rasterize individual pages on demand.
Text extraction gives you bullet-point text but loses all layout.
Form-heavy documents:
→ Extract form field values programmatically first (see below). Rasterize
the form page for visual context if needed.
Data-heavy documents (tables, charts, figures):
→ Use pdfplumber for tables. Rasterize pages with charts/figures.
Extract text for surrounding narrative. Consider both text AND image
for the same page when precision matters.
Extracting embedded images
# List all embedded images with metadata (size, color, compression)
pdfimages -list document.pdf
# Extract all images as PNG
pdfimages -png document.pdf /tmp/img
# Extract from specific pages only (pages 3-5)
pdfimages -png -f 3 -l 5 document.pdf /tmp/img
# Extract in original format (JPEG stays JPEG, etc.)
pdfimages -all document.pdf /tmp/imgThen Re
