Two things annoy me about OCR on PDFs. The first is paying OCR prices for pages that never needed OCR. The second is Arabic text that comes back with the words in the wrong order, so you cannot search it or hand it to a language model.

This post covers an Apify actor I publish as cyrus_1 that deals with both. I built it with an AI-assisted workflow: a small team of AI agents builds, tests and documents each actor, and I review and approve the result. This is a tutorial from its author, so read it with that in mind.

What it does

Image OCR: Text from Images and Scanned PDFs takes public links to PNG, JPEG, WebP or TIFF images and to PDFs. You get one JSON row per image, per TIFF frame and per PDF page. Each row has the joined text, and for OCR rows every line with its confidence and a four-corner box. The engine is RapidOCR with PaddleOCR models, running on CPU inside the actor. There is no login, no key for another service and no proxy.

Trick 1: read the text layer first

A PDF is either a picture of a page (a scan) or a file that holds its characters (a text layer). Many PDFs are a mix. A typed contract with a scanned signature page is the usual example.

The actor checks each PDF page. If the page already holds at least 40 letters or digits of its own text, it reads that text and skips OCR. Only pages without a text layer go through OCR.

The price difference is the point:

  • A page read from its text layer: $1 per 1,000 pages ($0.001).
  • A page read by OCR where text is found: $4 per 1,000 pages ($0.004).

A real example: I ran the default input, which is a public IRS Form W-9. That PDF has a text layer, so the run read 6 text-layer pages and charged 6 text-layer-page events, with no OCR. That is $0.006 for the whole form.

Text-layer rows have "textSource": "text-layer", and they have no line boxes and no confidence, because nothing was recognised. OCR rows have "textSource": "ocr". If a scan carries a poor hidden text layer, set usePdfTextLayer to false and every page is OCRed. A page whose hidden layer is only a stamp or a page number is below the 40-character bar, so it is OCRed as normal.

Trick 2: real OCR for scans and images, with lines, confidence and boxes

For pages with no text layer, and for plain images, the actor runs OCR. This is one row from a run with the text layer turned off, on page 1 of that same W-9 (text shortened, first of 103 lines shown):

{ "rowType": "ocr", "sourceType": "pdf", "textSource": "ocr", "pageNumber": 1, "pageCount": 6, "status": "ok", "language": "en", "text": "W-9\nRequest for Taxpayer\nForm\nGive form to the\n(Rev. March 2024)\nIdentification Number and Certification\n…", "lineCount": 103, "averageConfidence": 0.9835, "lines": [ { "text": "W-9", "confidence": 0.9923, "box": [[108, 64], [201, 64], [201, 115], [108, 115]], "words": null } ], "ocrSeconds": 2.957, "charges": { "image": 1 } } 

Boxes are in pixels of the image. For PDF pages they are in the rendered page, whose long side is about 1,600 px. Each line has its own confidence, and each row has an averageConfidence. The minConfidence input (default 0.5) drops lines the engine is less sure of. Turn on includeWordBoxes if you also want the position of every word.

That W-9 is a clean, typed form, so 0.98 is an easy case. Do not expect that on a blurry photo.

Arabic, Persian and Urdu in the right order

Set "language": "arabic". That recogniser covers Arabic script, so Arabic, Persian and Urdu.

What the actor does about order: text comes back in logical order, which is the order you would type it. The boxes of one line are listed right to left and joined into one text line. A line the detector split into two boxes reads as one. Numbers keep their place after the words they follow. With includeWordBoxes on, the words are in reading order too. That makes the text searchable, copyable and usable in a prompt without a fix-up step.

I tested the ordering on generated Arabic pages, including lines with numbers. I also ran an Arabic job on Apify: it came back with 13 lines and was charged 1 image event.

Now the limits, stated plainly:

  • Accuracy on old or degraded print is limited. The recogniser is a general one. It can misread letters in small or decorative print, and a photographed old newspaper page will not read like a clean page. Check averageConfidence and read a sample of your own files before you trust the output.
  • Latin words inside an Arabic line are often skipped. Numbers are read, but a mixed line may lose its Latin word.
  • Text-layer pages keep the order the PDF stores. For Arabic PDFs that order is not something I checked. For those, set usePdfTextLayer to false to OCR the pages.
  • Dense Arabic pages cost more. Above 40 lines, each extra block of 40 lines adds $0.004 (for English the threshold is 150 lines).
  • One run uses one language.

I would call the order correct and the accuracy "good on clean print, uncertain on bad print". The word "perfect" would be wrong.

Any source: Drive, Dropbox, ZIPs, datasets

You do not need to host files somewhere special. The urls field takes:

  • Plain public image and PDF links.
  • Google Drive and Dropbox share links. They must be public ("anyone with the link"). The actor turns them into direct-download links, and Docs, Slides and Sheets links are exported as PDF. A Drive folder link is refused. A Dropbox folder link is downloaded as a ZIP.
  • ZIP archives. They are unpacked in memory, with limits on file count (200), unpacked size (500 MB) and compression ratio. Each readable file inside is charged like a file you gave directly.

Other inputs:

  • datasetId plus datasetField: read links from a dataset of yours, for example the output of another actor.
  • urlListUrls: links to a TXT file (one link per line) or a CSV file that lists links.
  • base64Files: files sent as base64 text.
  • keyValueStoreId plus keyValueStoreKeys: files stored in an Apify key-value store.
  • followPdfLinks: give it a web page and it reads the page for links ending in .pdf, one level deep.

Private folders and links that need a login come back as free error rows.

Call it from Python

Install the client with pip install "apify-client>=3.5". Keep the token in an environment variable.

import os from apify_client import ApifyClient client = ApifyClient(os.environ["APIFY_TOKEN"]) run = client.actor("cyrus_1/image-ocr-text-extractor").call( run_input={ "urls": ["https://example.com/your-scanned-file.pdf"], "language": "arabic", "pages": "1-3", "minConfidence": 0.5, } ) for row in client.dataset(run.default_dataset_id).iterate_items(): if row.get("status") != "ok": print("failed:", row.get("url"), row.get("errorCode")) continue if row.get("textSource") == "text-layer": print(row["pageNumber"], "from the text layer, no confidence score") else: weak = [ line["text"] for line in row.get("lines") or [] if (line.get("confidence") or 0) < 0.8 ] print(row["pageNumber"], row.get("averageConfidence"), "weak lines:", len(weak)) print((row.get("text") or "")[:80]) 

Replace the example link with your own public PDF or image. The or [] guards matter: text-layer rows have no lines, and error rows have no text.

What it costs

Prices from the actor's README:

What Price
PDF page read from its text layer $1 per 1,000 ($0.001)
Image or page read by OCR where text is found $4 per 1,000 ($0.004)
Page or image read but no text above the confidence limit $1 per 1,000
Dense page +$0.004 per extra block of lines: for English, Chinese and Japanese one per 100 lines above 150; for Arabic and the other downloaded scripts one per 40 lines above 40
Accurate mode (English, Chinese, Japanese) +$0.004 per image or page
Failed downloads, files that are not images or PDFs, error rows free
Apify run-start charge very small, shown on the Pricing tab

Worked examples from the README, using Apify's free $5 of monthly credit:

  • A 6-page scanned form, fast mode: 6 x $0.004 = $0.024, so $5 covers about 208 forms.
  • A 6-page PDF that already has a text layer: 6 x $0.001 = $0.006, so $5 covers about 833.
  • About 1,250 single images with text.

Each row shows what it was charged in charges, so you can reconcile it with your bill. To cap spending, set a maximum cost per run in the run options.

Other limits

  • Tables, columns and layout are not rebuilt. You get lines with boxes and rebuild the layout from them if you need it.
  • Large images are shrunk to 4,000 px on the long side before reading. Images over 50 megapixels are refused.
  • GIF and BMP are not accepted. Password-protected PDFs are not opened.
  • Public links only. A site that blocks Apify's servers gives a free error row.
  • Files are read in memory and dropped. Nothing is kept except the output dataset in your own account.

Try it

The actor is at https://apify.com/cyrus_1/image-ocr-text-extractor. Run it with the default input first: it reads page 1 of a public IRS form.

One question for you: if you work with Arabic documents, what kind of source is hardest for you, scans, photos or old print? I would like to test against real examples.

*Disclosure: I wrote this with help from AI tools. The numbers come from the actor's README and from real runs, and the code uses the official apify-client.