Use OCR when a PDF contains scanned pages or images without an extractable text layer. For born-digital documents, try a local text extractor first. The runnable example below lets you compare one synthetic PDF with the actual Markdown returned by PennyOCR.
from pennyocr import PennyOCR
result = PennyOCR().ocr("report.pdf") # markdown is the default
print(result.text)
03 / KEEP PAGE BOUNDARIES (FOR CITATIONS)
page_results carries per-page markdown, so chunks can cite real page numbers:
for page in result.page_results:
print(f"--- page {page.page} ---")
print(page.text[:200])
04 / STRAIGHT INTO A RAG PIPELINE
For this optional step, install langchain-text-splitters first. Split Markdown headings into chunks, then check the output and page references before ingestion:
Start with this one-page synthetic invoice. The returned Markdown below is from a live public-demo request on October 20, 2026. It retained invoice DEMO-1001, both line items and the USD 42.00 total. This is one reproducible example, not an accuracy benchmark.
# Python 3.10+. No dependencies. Download convert.py and sample-invoice.pdf above.
python3 convert.py sample-invoice.pdf --demo
# For your own documents and normal API limits:
export PENNYOCR_API_KEY=YOUR_KEY
python3 convert.py your-document.pdf --output ocr-output
The public demo reads the first PDF page only, has a 10 MB cap and a daily quota. The authenticated API accepts up to 50 MB and 500 pages per request. The script writes Markdown and the complete JSON response, including page results when returned. It does not retry requests automatically.
VIEW THE ACTUAL RETURNED MARKDOWN
# SYNTHETIC INVOICE
Test document only. No real customer or payment details.
**Sample Parts Company**
Invoice: DEMO-1001
Date: October 20, 2026
Bill to: Example Automation Team
| Item | Quantity | Unit price (USD) | Amount (USD) |
|---|---:|---:|---:|
| Widget | 2 | 12.00 | 24.00 |
| Gasket | 3 | 6.00 | 18.00 |
Subtotal: USD 42.00
Tax: USD 0.00
**TOTAL: USD 42.00**
This sample tests a heading, invoice identifier, line items and a total.
Compare extracted text to the original. OCR output is not accounting validation.
This request took 43.22 seconds end to end, including the upload and response. One measurement does not establish typical speed. Latency and extraction quality depend on the document and available engine. Check your own samples before choosing a production pipeline.