{"slug": "ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order", "title": "OCR for scanned PDFs and images, including Arabic in the right order", "summary": "A developer publishing on Apify as cyrus_1 released an OCR actor that checks each PDF page for an existing text layer and skips OCR when a page already contains at least 40 letters or digits, charging a cheaper text-layer-page event instead. The actor runs RapidOCR with PaddleOCR models on CPU and returns per-line confidence scores and bounding boxes, and it handles Arabic script (including Persian and Urdu) by returning text in logical order with right-to-left line boxes joined into single lines. In a test on an IRS Form W-9 with a text layer, the run read six pages without OCR for $0.006 total.", "body_md": "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.\n\nThis 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.\n\n[Image OCR: Text from Images and Scanned PDFs](https://apify.com/cyrus_1/image-ocr-text-extractor) 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.\n\nA 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.\n\nThe 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.\n\nThe price difference is the point:\n\nA 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.\n\nText-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.\n\nFor 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):\n\n```\n{\n  \"rowType\": \"ocr\",\n  \"sourceType\": \"pdf\",\n  \"textSource\": \"ocr\",\n  \"pageNumber\": 1,\n  \"pageCount\": 6,\n  \"status\": \"ok\",\n  \"language\": \"en\",\n  \"text\": \"W-9\\nRequest for Taxpayer\\nForm\\nGive form to the\\n(Rev. March 2024)\\nIdentification Number and Certification\\n…\",\n  \"lineCount\": 103,\n  \"averageConfidence\": 0.9835,\n  \"lines\": [\n    {\n      \"text\": \"W-9\",\n      \"confidence\": 0.9923,\n      \"box\": [[108, 64], [201, 64], [201, 115], [108, 115]],\n      \"words\": null\n    }\n  ],\n  \"ocrSeconds\": 2.957,\n  \"charges\": { \"image\": 1 }\n}\n```\n\nBoxes 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.\n\nThat W-9 is a clean, typed form, so 0.98 is an easy case. Do not expect that on a blurry photo.\n\nSet `\"language\": \"arabic\"`. That recogniser covers Arabic script, so Arabic, Persian and Urdu.\n\nWhat 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.\n\nI 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.\n\nNow the limits, stated plainly:\n\n`averageConfidence` and read a sample of your own files before you trust the output.`usePdfTextLayer` to `false` to OCR the pages.\nI would call the order correct and the accuracy \"good on clean print, uncertain on bad print\". The word \"perfect\" would be wrong.\n\nYou do not need to host files somewhere special. The `urls` field takes:\n\nOther inputs:\n\n`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.\nPrivate folders and links that need a login come back as free error rows.\n\nInstall the client with `pip install \"apify-client>=3.5\"`. Keep the token in an environment variable.\n\n``` python\nimport os\nfrom apify_client import ApifyClient\n\nclient = ApifyClient(os.environ[\"APIFY_TOKEN\"])\n\nrun = client.actor(\"cyrus_1/image-ocr-text-extractor\").call(\n    run_input={\n        \"urls\": [\"https://example.com/your-scanned-file.pdf\"],\n        \"language\": \"arabic\",\n        \"pages\": \"1-3\",\n        \"minConfidence\": 0.5,\n    }\n)\n\nfor row in client.dataset(run.default_dataset_id).iterate_items():\n    if row.get(\"status\") != \"ok\":\n        print(\"failed:\", row.get(\"url\"), row.get(\"errorCode\"))\n        continue\n    if row.get(\"textSource\") == \"text-layer\":\n        print(row[\"pageNumber\"], \"from the text layer, no confidence score\")\n    else:\n        weak = [\n            line[\"text\"]\n            for line in row.get(\"lines\") or []\n            if (line.get(\"confidence\") or 0) < 0.8\n        ]\n        print(row[\"pageNumber\"], row.get(\"averageConfidence\"), \"weak lines:\", len(weak))\n    print((row.get(\"text\") or \"\")[:80])\n```\n\nReplace 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.\n\nPrices from the actor's README:\n\n| What | Price | \n|---|---|\n| PDF page read from its text layer | $1 per 1,000 ($0.001) | \n| Image or page read by OCR where text is found | $4 per 1,000 ($0.004) | \n| Page or image read but no text above the confidence limit | $1 per 1,000 | \n| 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 | \n| Accurate mode (English, Chinese, Japanese) | +$0.004 per image or page | \n| Failed downloads, files that are not images or PDFs, error rows | free | \n| Apify run-start charge | very small, shown on the Pricing tab | \n\nWorked examples from the README, using Apify's free $5 of monthly credit:\n\nEach 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.\n\nThe actor is at [https://apify.com/cyrus_1/image-ocr-text-extractor](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.\n\nOne 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.\n\n*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`.", "url": "https://wpnews.pro/news/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order", "canonical_source": "https://dev.to/cyrus_1/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order-46dd", "published_at": "2026-11-24 16:32:40+00:00", "updated_at": "2026-11-24 16:47:31.852340+00:00", "lang": "en", "topics": ["ai-tools", "natural-language-processing", "computer-vision", "ai-products"], "entities": ["Apify", "RapidOCR", "PaddleOCR", "cyrus_1"], "also_reported_by": [], "takeaway": "Checking each PDF page for at least 40 characters of embedded text before OCR cut a six-page IRS W-9 to $0.006 with zero OCR calls, while Arabic pages return logical-order text with right-to-left line boxes merged into single lines.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order", "markdown": "https://wpnews.pro/news/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order.md", "text": "https://wpnews.pro/news/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order.txt", "jsonld": "https://wpnews.pro/news/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order.jsonld"}}