Using an OCR API on PDF files
First check whether the PDF needs OCR at all: if you can select its text, it already has a text layer, and a PDF reader extracts it exactly, without OCR mistakes. Scanned PDFs are pictures and need OCR page by page. Our OCR API reads images, not PDF files, so turn each page into a PNG and send them one at a time ($0.03 per page); for text PDFs, use our page reader instead ($0.02 per file).
Try it free Image to Text API (OCR) — read text from photos and scans · $0.03 / call
Text PDF or scanned PDF?
- Try to select a sentence in your PDF viewer. If you can, it's a text PDF.
- Run pdffonts file.pdf (from poppler). A pure scan lists no fonts at all.
- Run pdftotext file.pdf - | head. Empty output, or a few stray characters, means scan.
- Mixed files exist: a typed contract with a scanned signature page, for example.
Scanned PDF: pages to images, then OCR
pdftoppm turns each page into a PNG. 200 dpi is a good balance between legibility and file size; each image must stay under 5 MB.
pdftoppm -r 200 -png scan.pdf page # page-01.png, page-02.png, ...
import base64, glob
OCR = "https://iamai.markets/api/x402/services/z2uYlMYGy6Xs"
pages = []
for path in sorted(glob.glob("page-*.png")):
b64 = base64.b64encode(open(path, "rb").read()).decode()
# pay_session: a requests session wrapped by your x402 v2 client
r = pay_session.post(OCR, json={"image_base64": b64, "mime_type": "image/png"})
r.raise_for_status()
pages.append(r.json()["result"]["text"])
open("scan.txt", "w").write("\n\n".join(pages))
Or make the PDF searchable instead
If you want to keep the PDF and be able to search it, ocrmypdf (open source, uses Tesseract) adds an invisible text layer on your own computer: ocrmypdf scan.pdf searchable.pdf. After that it behaves like a text PDF, and any PDF reader, ours included, can extract the text.
What a document costs
| Document | Route | Cost with our APIs |
|---|---|---|
| 10-page text PDF | Page reader, one call | $0.02 |
| 10-page scanned PDF | OCR, one call per page | $0.30 |
| 100-page scanned PDF | OCR, one call per page | $3.00 |
| Mixed: 8 text pages, 2 scanned | Reader for the file, OCR for the 2 pages | $0.08 |
PDF OCR options
| Option | Takes PDF files directly | Notes |
|---|---|---|
| Google Cloud Vision | Yes, as an async job via Cloud Storage | Google Cloud account |
| Amazon Textract | Yes, async via S3 | Also reads forms and tables |
| Azure AI Document Intelligence | Yes | Prebuilt models for invoices and IDs |
| ocrmypdf | Yes | Free and local; Tesseract quality |
| OCR API (iamai.markets) | No: one image per call | $0.03 per page, no account |
The product
Questions
Can I send the PDF file itself to the OCR API?
No. It accepts PNG, JPEG, WebP or GIF images. Split the PDF into page images first, as shown above.
What resolution should the page images be?
200 to 300 dpi for normal text. Go higher only for very small print, and check the file stays under 5 MB.
How do I handle a mixed PDF?
Read it with the page reader first. Pages that come back with no text are the scanned ones; send only those to OCR.
Related
Updated 2026-12-01
