* * * GUIDES * * *

Convert a PDF to Markdown in Python.

UPDATED

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.

01 / SETUP

pip install pennyocr
export PENNYOCR_API_KEY=pk_live_...

02 / CONVERT

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:

from langchain_text_splitters import MarkdownHeaderTextSplitter

splitter = MarkdownHeaderTextSplitter([("#", "h1"), ("##", "h2")])
chunks = splitter.split_text(result.text)

05 / BIG PDFS BY URL, WITH A COST CAP

For documents that live at a URL, skip the download and cap your spend up front:

import os, httpx

r = httpx.post("https://api.pennyocr.com/v1/ocr/url",
    headers={"Authorization": f"Bearer {os.environ['PENNYOCR_API_KEY']}"},
    json={"url": "https://example.com/big.pdf", "pages": "1-50", "max_cost_usd": 0.05}, timeout=120)
r.raise_for_status()
print(r.json()["text"][:500])

A 500-page PDF costs $0.375. Tables are preserved inside the markdown, so an LLM can read them as tables.

A WORKING STARTING POINT

Run a PDF. Inspect the Markdown.

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.