PDF Support
OpenOCR automatically handles PDFs — no extra configuration needed. Send a PDF the same way you send an image, and the API takes care of the rest.
How It Works
When you send a PDF to the /v1/ocr endpoint, the router automatically detects it and handles conversion:
- Auto-detection — The router identifies PDFs by MIME type, magic bytes, or file extension. No need to specify a content type flag.
- Native vs. converted — Some engines (like OCRmyPDF) handle PDFs natively. For image-only engines (Tesseract, EasyOCR, docTR, Surya, RapidOCR), the router automatically converts each page to a PNG image at 200 DPI.
- Per-page processing — Each page is OCR'd individually, unless the PDF text layer (on by default,
options.pdf_text_layer) answers it from the PDF's own embedded text. Results are combined with page separators (---). - Aggregated results — Confidence scores are averaged across all pages. Cost is calculated per page.
Sending a PDF
Use the same /v1/ocr endpoint. PDFs can be sent as base64 or via URL, just like images.
How a PDF is recognized. For an engine with native_pdf true the provider reads the PDF itself. For every other engine, pdf means the router rasterizes the PDF first. It recognizes a PDF only when the input is base64 with mime_type application/pdf (or data starting with the %PDF magic), or a URL whose path ends in .pdf (query string ignored). An extensionless PDF URL such as /download/123 is not detected and is sent to the engine as an image: download it and send base64, or use a .pdf URL.
Every page is read unless you set page_count. A PDF sent as base64 without it is read whole, up to the limit of the engine you chose (each engine has its own maximum; see max_pages_per_request in GET /v1/engines), and you pay for each page read.page_count is an optional cap: send it to read only the first pages. If your balance cannot cover every page, the request is refused with 402 before anything is read or charged. A PDF sent by URL is not counted before it is read, and neither is a PDF the router cannot parse in time; both read page 1 only, so send page_count whenever you need a known number of pages.
# PDF via base64
curl -X POST https://api.open-ocr.com/v1/ocr \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-ocr-your-api-key" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"engine": "mistral-mistral-ocr-4-1",
"input": {
"type": "base64",
"data_base64": "<your-pdf-base64>",
"mime_type": "application/pdf"
}
}'
# PDF via URL: send page_count to read more than page 1
curl -X POST https://api.open-ocr.com/v1/ocr \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-ocr-your-api-key" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"engine": "openocr-ocrmypdf",
"page_count": 10,
"input": {
"type": "url",
"url": "https://example.com/document.pdf"
}
}'Python Example
import base64
import uuid
import requests
# Read a local PDF
with open("document.pdf", "rb") as f:
pdf_b64 = base64.b64encode(f.read()).decode()
response = requests.post(
"https://api.open-ocr.com/v1/ocr",
headers={
"Authorization": "Bearer sk-ocr-your-api-key",
"Idempotency-Key": str(uuid.uuid4()) # new UUID per logical request
},
json={
"engine": "mistral-mistral-ocr-4-1",
# no page_count: every page is read; add "page_count": 3 to read only pages 1-3
"input": {
"type": "base64",
"data_base64": pdf_b64,
"mime_type": "application/pdf"
}
}
)
response.raise_for_status() # an error envelope has no "status" field
result = response.json()
if result["status"] != "succeeded":
raise RuntimeError(f"OCR ended as {result['status']}: {result.get('provider_error')}")
print(result["extracted_text"])Multi-Page Output
For multi-page PDFs, the extracted text from each page is separated by --- (three dashes on their own line). You can split on this delimiter to get per-page text:
JSON responses report pages (processed and billed) and pages_total. With output_format text or markdown, the same information arrives in the X-OCR-Pages, X-OCR-Pages-Total and X-OCR-Warnings headers (warnings joined with " | "; the header is absent when nothing was skipped).
# result is the parsed JSON body of a successful response
result = {"extracted_text": "Page one text\n---\nPage two text"}
pages = result["extracted_text"].split("\n---\n")
for i, page_text in enumerate(pages):
print(f"Page {i + 1}: {len(page_text)} chars")Engine Compatibility
| Engine | PDF Handling | Notes |
|---|---|---|
| openocr-ocrmypdf | Native | Processes PDFs directly. Best for scanned documents. |
| local-tesseract | Auto-converted | PDF pages converted to 200 DPI images automatically. |
| local-easyocr | Auto-converted | PDF pages converted to 200 DPI images automatically. |
| local-doctr | Auto-converted | PDF pages converted to 200 DPI images automatically. |
| openocr-rapidocr | Auto-converted | PDF pages converted to 200 DPI images automatically. |
Limits
- Maximum 100 pages per PDF (configurable server-side)
- Render resolution: 200 DPI (balances quality vs. speed)
- Pages your engine reads are billed at its per-page price. A page answered from the PDF's embedded text is billed at the PDF Text Layer price (0.01¢ a page);
metadata.text_layersays which pages those were - More than 20 pages on an engine without native PDF support runs as a background job and returns 202 (see Large PDFs)