Extract PDF text page by page from the embedded text layer, detect scanned pages, and run OCR only where needed with ocrmypdf or tesseract.
---
name: pdf-text-extractor
description: Extracts text from PDF files, page by page, as plain text or JSON. Reads the embedded text layer first with pdftotext or pypdf, detects pages that are scanned images, and only then runs OCR with ocrmypdf or tesseract if installed. Use when asked to pull text out of a PDF, convert a PDF to text, read an invoice or report PDF, or check whether a PDF is searchable.
compatibility: Needs local file access and one of pdftotext (Poppler or Xpdf) or Python with pypdf. OCR of scanned pages additionally needs ocrmypdf or tesseract with the right language data.
metadata:
version: "1.0.0"
category: "document-processing"
---
# PDF Text Extractor
Most PDFs created from software already contain a text layer, which can be extracted exactly without OCR. OCR is only needed for pages that are images (scans, photos, faxes), and its output can contain recognition errors. This skill decides per page which path applies and reports it.
## Inputs to collect
- Path to the PDF (ask before downloading one from a URL; treat downloaded files as untrusted).
- Page range, if not the whole document.
- Output: plain text (default), text with layout preserved, or JSON per page.
- Language of scanned pages, if OCR may be needed (for example `eng`, `deu`, `fra`).
## Workflow
1. **Check the file.** `pdfinfo in.pdf` (page count, encryption) if Poppler is installed, or open it with pypdf. If it is encrypted, ask for the password or an unlocked copy; do not try to break it.
2. **Extract the text layer.**
- Poppler/Xpdf: `pdftotext -layout -f 1 -l 5 in.pdf out.txt` (`-layout` keeps columns; omit it for reading order). Use `-` as the output file to print to stdout.
- Python fallback (`pip install pypdf` in a virtual environment):
```python
import sys
from pypdf import PdfReader
reader = PdfReader(sys.argv[1])
if reader.is_encrypted:
sys.exit("Encrypted PDF: ask the user for the password or an unlocked copy")
for n, page in enumerate(reader.pages, 1):
text = page.extract_text() or ""
status = "text" if len(text.strip()) >= 20 else "needs-ocr"
print(f"--- page {n} [{status}] ---")
print(text)
```
3. **Classify pages.** A page with little or no extracted text but visible content is probably an image. Mark it `needs-ocr` rather than returning an empty page silently. The 20-character threshold above is a heuristic; adjust it for pages that are legitimately short.
4. **OCR only the image pages, if a tool is installed and the user agrees** (OCR is slower and approximate):
```bash
ocrmypdf --skip-text -l eng in.pdf searchable.pdf # newer releases also accept --mode skip
pdftotext -layout searchable.pdf out.txt
```
`--skip-text` leaves pages that already have text untouched. Without ocrmypdf, rasterize the page and run `tesseract page.png out -l eng`. If no OCR tool is available, say which pages need OCR and stop.
5. **Return the result** with page markers and a summary: pages extracted from the text layer, pages OCRed, pages skipped, and any warnings.
## Output format
```
Source: invoice.pdf (2 pages, not encrypted)
Method: text layer (pypdf) for pages 1-2; no OCR needed
--- page 1 ---
Invoice 2026-118 Total due: 1,240.00 EUR
--- page 2 ---
Payment terms: 30 days
```
JSON variant: `[{"page": 1, "method": "text-layer", "text": "..."}, ...]`
## Limitations
- Tables, multi-column layouts and headers/footers may come out in an unexpected order; say so and offer `-layout` output.
- Ligatures, hyphenation and custom font encodings can produce odd characters; report them rather than silently "correcting" numbers or names.
- OCR text must be presented as OCR output; recommend checking amounts, dates and names against the page image.
- Do not send the document to an online OCR or AI service without the user's permission.
Generate a GitHub Actions (or other CI) pipeline from the repo's real scripts, with least-privilege permissions, caching, concurrency and a gated deploy.
Generate a compose.yaml for your app, database and cache with health-checked startup, named volumes and secrets in .env, validated with docker compose config.
Automate notifications and alerts
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
An accounts assistant needs the text of a two-page supplier invoice PDF and a separate scanned receipt to copy figures into a spreadsheet.
Extract the text from ~/Downloads/invoice.pdf and ~/Downloads/scan.pdf. Tell me which pages needed OCR.
For invoice.pdf (software-generated): method 'text layer', page 1 'Invoice 2026-118 Total due: 1,240.00 EUR', page 2 'Payment terms: 30 days', no OCR. For scan.pdf (an image-only page): page 1 marked needs-ocr with no text extracted; if ocrmypdf is installed and you agree, a searchable copy is produced and its text returned labeled as OCR output; if not, the skill says OCR is required and which tool to install, without inventing text.
AI Agent Skills are reusable workflow packages that extend your AI coding assistant with multi-step capabilities. Install PDF Text Extractor by following these steps.
SKILL.md inside, in the location documented by your client. Do not assume every AI application reads the same directory.Requires an Agent Skills-compatible client; supported locations and activation vary by version. Read the SKILL.md setup guide. MCP connects tools and data and does not, by itself, install a skill.
Generate a GitHub Actions (or other CI) pipeline from the repo's real scripts, with least-privilege permissions, caching, concurrency and a gated deploy.
Generate a compose.yaml for your app, database and cache with health-checked startup, named volumes and secrets in .env, validated with docker compose config.
Automate notifications and alerts