DEV Community

Mohit Sawhney
Mohit Sawhney

Posted on

Extract text from scanned PDFs in one API call (and only pay OCR prices for scanned pages)

Most PDFs you get are a mix: some pages contain real text, others are scans. Running OCR on every page is slow and costs more than it needs to.

Here's one request that handles both. Pages that already contain text are read directly; only scanned pages and images go through OCR.

curl -X POST \
  "https://api.apify.com/v2/acts/mvisiondigital~pdf-image-ocr/run-sync-get-dataset-items?token=YOUR_APIFY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"fileUrls": ["https://example.com/contract.pdf"]}'
Enter fullscreen mode Exit fullscreen mode

You get one row per page:

{"page": 2, "method": "ocr", "text": "This Agreement is made on ...", "confidence": 0.96, "fileTextUrl": "https://..."}
Enter fullscreen mode Exit fullscreen mode

method tells you whether the page was read directly (text-layer) or OCR'd. fileTextUrl links to the whole document as one text file, which is handy for feeding a RAG pipeline.

In Python:

from apify_client import ApifyClient

client = ApifyClient("YOUR_APIFY_TOKEN")
run = client.actor("mvisiondigital/pdf-image-ocr").call(run_input={"fileUrls": ["https://example.com/contract.pdf"]})
for page in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(page["page"], page["method"], page["text"][:80])
Enter fullscreen mode Exit fullscreen mode

Pricing is $4 per 1,000 scanned pages and $0.50 per 1,000 digital pages, with no subscription. If you already use RapidAPI, the same OCR is there too, with a free plan: https://rapidapi.com/MohitSawhney-YfHq0dWUO/api/audio-noise-remover-ocr-and-video-frames

Disclosure: I built it.

More: https://mvision-apis.web.app

Top comments (0)