<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohamed Ayman</title>
    <description>The latest articles on DEV Community by Mohamed Ayman (@cyrus_1).</description>
    <link>https://dev.to/cyrus_1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4262948%2Ffc4c795e-190b-4793-b172-cd65c795a5c2.png</url>
      <title>DEV Community: Mohamed Ayman</title>
      <link>https://dev.to/cyrus_1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cyrus_1"/>
    <language>en</language>
    <item>
      <title>OCR for scanned PDFs and images, including Arabic in the right order</title>
      <dc:creator>Mohamed Ayman</dc:creator>
      <pubDate>Tue, 24 Nov 2026 16:32:40 +0000</pubDate>
      <link>https://dev.to/cyrus_1/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order-46dd</link>
      <guid>https://dev.to/cyrus_1/ocr-for-scanned-pdfs-and-images-including-arabic-in-the-right-order-46dd</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This post covers an Apify actor I publish as &lt;code&gt;cyrus_1&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it does
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://apify.com/cyrus_1/image-ocr-text-extractor" rel="noopener noreferrer"&gt;Image OCR: Text from Images and Scanned PDFs&lt;/a&gt; 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 &lt;code&gt;text&lt;/code&gt;, and for OCR rows every line with its &lt;code&gt;confidence&lt;/code&gt; and a four-corner &lt;code&gt;box&lt;/code&gt;. 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trick 1: read the text layer first
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The price difference is the point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A page read from its text layer: $1 per 1,000 pages ($0.001).&lt;/li&gt;
&lt;li&gt;A page read by OCR where text is found: $4 per 1,000 pages ($0.004).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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 &lt;code&gt;text-layer-page&lt;/code&gt; events, with no OCR. That is $0.006 for the whole form.&lt;/p&gt;

&lt;p&gt;Text-layer rows have &lt;code&gt;"textSource": "text-layer"&lt;/code&gt;, and they have no line boxes and no confidence, because nothing was recognised. OCR rows have &lt;code&gt;"textSource": "ocr"&lt;/code&gt;. If a scan carries a poor hidden text layer, set &lt;code&gt;usePdfTextLayer&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trick 2: real OCR for scans and images, with lines, confidence and boxes
&lt;/h2&gt;

&lt;p&gt;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):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"rowType"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ocr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"sourceType"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"textSource"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ocr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"pageNumber"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"pageCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"W-9\nRequest for Taxpayer\nForm\nGive form to the\n(Rev. March 2024)\nIdentification Number and Certification\n…"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"lineCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;103&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"averageConfidence"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.9835&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"lines"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"W-9"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"confidence"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.9923&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"box"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;108&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;201&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;115&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;108&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;115&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"words"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"ocrSeconds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;2.957&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"charges"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"image"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;confidence&lt;/code&gt;, and each row has an &lt;code&gt;averageConfidence&lt;/code&gt;. The &lt;code&gt;minConfidence&lt;/code&gt; input (default 0.5) drops lines the engine is less sure of. Turn on &lt;code&gt;includeWordBoxes&lt;/code&gt; if you also want the position of every word.&lt;/p&gt;

&lt;p&gt;That W-9 is a clean, typed form, so 0.98 is an easy case. Do not expect that on a blurry photo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arabic, Persian and Urdu in the right order
&lt;/h2&gt;

&lt;p&gt;Set &lt;code&gt;"language": "arabic"&lt;/code&gt;. That recogniser covers Arabic script, so Arabic, Persian and Urdu.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;includeWordBoxes&lt;/code&gt; on, the words are in reading order too. That makes the text searchable, copyable and usable in a prompt without a fix-up step.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;image&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;Now the limits, stated plainly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Accuracy on old or degraded print is limited.&lt;/strong&gt; 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 &lt;code&gt;averageConfidence&lt;/code&gt; and read a sample of your own files before you trust the output.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Latin words inside an Arabic line are often skipped.&lt;/strong&gt; Numbers are read, but a mixed line may lose its Latin word.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Text-layer pages keep the order the PDF stores.&lt;/strong&gt; For Arabic PDFs that order is not something I checked. For those, set &lt;code&gt;usePdfTextLayer&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; to OCR the pages.&lt;/li&gt;
&lt;li&gt;Dense Arabic pages cost more. Above 40 lines, each extra block of 40 lines adds $0.004 (for English the threshold is 150 lines).&lt;/li&gt;
&lt;li&gt;One run uses one language.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would call the order correct and the accuracy "good on clean print, uncertain on bad print". The word "perfect" would be wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Any source: Drive, Dropbox, ZIPs, datasets
&lt;/h2&gt;

&lt;p&gt;You do not need to host files somewhere special. The &lt;code&gt;urls&lt;/code&gt; field takes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plain public image and PDF links.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other inputs:&lt;/p&gt;

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

&lt;p&gt;Private folders and links that need a login come back as free error rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Call it from Python
&lt;/h2&gt;

&lt;p&gt;Install the client with &lt;code&gt;pip install "apify-client&amp;gt;=3.5"&lt;/code&gt;. Keep the token in an environment variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;apify_client&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ApifyClient&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ApifyClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"APIFY_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cyrus_1/image-ocr-text-extractor"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;run_input&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"urls"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://example.com/your-scanned-file.pdf"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"arabic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1-3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"minConfidence"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default_dataset_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;iterate_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"failed:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"errorCode"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"textSource"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"text-layer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"pageNumber"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"from the text layer, no confidence score"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;weak&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"lines"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"confidence"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.8&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"pageNumber"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"averageConfidence"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"weak lines:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weak&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h2&gt;
  
  
  What it costs
&lt;/h2&gt;

&lt;p&gt;Prices from the actor's README:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PDF page read from its text layer&lt;/td&gt;
&lt;td&gt;$1 per 1,000 ($0.001)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Image or page read by OCR where text is found&lt;/td&gt;
&lt;td&gt;$4 per 1,000 ($0.004)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Page or image read but no text above the confidence limit&lt;/td&gt;
&lt;td&gt;$1 per 1,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dense page&lt;/td&gt;
&lt;td&gt;+$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&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accurate mode (English, Chinese, Japanese)&lt;/td&gt;
&lt;td&gt;+$0.004 per image or page&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Failed downloads, files that are not images or PDFs, error rows&lt;/td&gt;
&lt;td&gt;free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Apify run-start charge&lt;/td&gt;
&lt;td&gt;very small, shown on the Pricing tab&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Worked examples from the README, using Apify's free $5 of monthly credit:&lt;/p&gt;

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

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

&lt;h2&gt;
  
  
  Other limits
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;The actor is at &lt;a href="https://apify.com/cyrus_1/image-ocr-text-extractor" rel="noopener noreferrer"&gt;https://apify.com/cyrus_1/image-ocr-text-extractor&lt;/a&gt;. Run it with the default input first: it reads page 1 of a public IRS form.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;*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 &lt;code&gt;apify-client&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>ocr</category>
      <category>pdf</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>I built a bulk domain checker. DNS answers most names, and one problem is still open</title>
      <dc:creator>Mohamed Ayman</dc:creator>
      <pubDate>Tue, 24 Nov 2026 16:00:05 +0000</pubDate>
      <link>https://dev.to/cyrus_1/i-built-a-bulk-domain-checker-dns-answers-most-names-and-one-problem-is-still-open-hck</link>
      <guid>https://dev.to/cyrus_1/i-built-a-bulk-domain-checker-dns-answers-most-names-and-one-problem-is-still-open-hck</guid>
      <description>&lt;p&gt;I built a small tool that checks a long list of domain names and says which ones are registered and which look free. This is how it works and the one problem I have not solved.&lt;/p&gt;

&lt;p&gt;It is an Apify actor, published under &lt;code&gt;cyrus_1&lt;/code&gt;. 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. The decisions below are the ones I made while reviewing it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with checking names one by one
&lt;/h2&gt;

&lt;p&gt;Checking name ideas at a registrar means pasting one name at a time. For a list of a few hundred ideas that is slow. I wanted one run that takes the whole list and returns a status per name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why DNS first
&lt;/h2&gt;

&lt;p&gt;The obvious design is to ask the registry about every name. I decided against that as the first step, for two reasons.&lt;/p&gt;

&lt;p&gt;A domain that has name servers in DNS is registered. That ends the question, and the check costs the registry nothing, because it is an ordinary DNS lookup. Every name answered this way is a name the registry never hears about.&lt;/p&gt;

&lt;p&gt;A registry lookup, by contrast, is a request to a service run by the registry, and registries have rules about high-volume automated queries. The README names one example: Verisign's RDAP terms for &lt;code&gt;.com&lt;/code&gt; and &lt;code&gt;.net&lt;/code&gt;. I did not want to build something that hammers a registry because it was easier.&lt;/p&gt;

&lt;p&gt;So the order is: DNS for everything. Only names with no DNS records go on to the registry.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why RDAP second
&lt;/h2&gt;

&lt;p&gt;No name servers does not mean the name is free. A domain can be registered and still have no DNS records. It might be parked, on hold, or just not set up yet. If I stopped at DNS, I would call those "available", and you would find out at the registrar checkout.&lt;/p&gt;

&lt;p&gt;RDAP is the registry's own lookup service. It answers "is this name registered?" with a clear yes or no. For registered names it can also give the registrar and the dates. So after DNS, names with no records are asked at the registry:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Registry says "not found": the tool reports &lt;code&gt;available&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Registry says it is registered (parked, on hold): &lt;code&gt;registered&lt;/code&gt;, with &lt;code&gt;source: rdap&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tool never reads or returns registrant or contact data. It only needs to know whether the name exists.&lt;/p&gt;

&lt;p&gt;A real run on three names shows the shape. Two were registered with name servers, so DNS decided and no registry was asked. One was a name that nobody has registered, and it came back &lt;code&gt;available&lt;/code&gt;. The run took 4.4 seconds.&lt;/p&gt;

&lt;p&gt;Here is one DNS-decided row from a real run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"domain"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"apify.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"registered"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"reason"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"has_nameservers"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"dns"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"rdapChecked"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The cap, and why it is there
&lt;/h2&gt;

&lt;p&gt;The per-run cap is the decision I weighed most. RDAP lookups are capped at 200 per run by default. You can raise it to 5,000, or set it to 0 to use DNS only.&lt;/p&gt;

&lt;p&gt;I chose a low default on purpose. The tool is meant for checking name candidates, not for harvesting every unregistered name. A cap makes the polite behaviour the default and the aggressive behaviour a choice you have to make.&lt;/p&gt;

&lt;p&gt;The other rules, from the README:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One request at a time per registry server, with a pause after each. The default is one second and it cannot go below half a second.&lt;/li&gt;
&lt;li&gt;If a server answers "too many requests", the tool stops asking it for the rest of that run. Its remaining names fall back to DNS.&lt;/li&gt;
&lt;li&gt;The list of registry servers comes from the IANA RDAP list, fetched once per run.&lt;/li&gt;
&lt;li&gt;Past the cap, names are answered from DNS alone as &lt;code&gt;likely_available&lt;/code&gt;, not &lt;code&gt;available&lt;/code&gt;. The word is different on purpose. It means "no DNS records, but no registry has confirmed it".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would rather the tool say "likely" than overstate. If you rely on a name, confirm it at a registrar.&lt;/p&gt;

&lt;h2&gt;
  
  
  Respecting registry terms
&lt;/h2&gt;

&lt;p&gt;I am not a lawyer, and I do not claim this makes any use fine. What I can say is what the tool does and does not do. It asks only whether a name is registered. It does not collect owner data. It limits itself by default and slows down when told to. The README says that RDAP data belongs to each registry and is subject to its terms, and that anyone who needs very large volumes should talk to the registries. I think that is the right way round.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem I have not solved
&lt;/h2&gt;

&lt;p&gt;Some top-level domains have no RDAP service at all. The README gives &lt;code&gt;.io&lt;/code&gt; and &lt;code&gt;.de&lt;/code&gt; as examples. For those, a name with no DNS records can only be &lt;code&gt;likely_available&lt;/code&gt;. The tool cannot do better, and for someone who wanted a &lt;code&gt;.io&lt;/code&gt; name that is not much help.&lt;/p&gt;

&lt;p&gt;There is also a pricing choice I am not sure about. A &lt;code&gt;likely_available&lt;/code&gt; row is charged the full $1.50 per 1,000, even though no registry confirmed it, because DNS still gave a verdict. &lt;code&gt;unknown&lt;/code&gt; and &lt;code&gt;invalid&lt;/code&gt; rows are free. I can see the argument for charging less for a row that only says "likely", and the argument against is that the DNS work was still done.&lt;/p&gt;

&lt;p&gt;My questions for you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How do you check availability for TLDs that have no RDAP, without scraping registrar sites?&lt;/li&gt;
&lt;li&gt;Would you pay full price for &lt;code&gt;likely_available&lt;/code&gt; rows?&lt;/li&gt;
&lt;li&gt;Is a default of 200 registry lookups too low or too high for your list?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  If you want to try it
&lt;/h2&gt;

&lt;p&gt;It is on Apify as &lt;a href="https://apify.com/cyrus_1/bulk-domain-availability-checker" rel="noopener noreferrer"&gt;Bulk Domain Availability Checker&lt;/a&gt;. It costs $1.50 per 1,000 domains, and Apify's free plan credit of $5 a month covers about 3,300 domains. A list of 500 name ideas costs at most $0.75. The default input runs a three-name example. No login or key is needed besides an Apify account.&lt;/p&gt;

&lt;p&gt;I made it, so weigh my opinion accordingly.&lt;/p&gt;

&lt;p&gt;*Disclosure: I wrote this with help from AI tools. The numbers come from the actor's README and from a real run.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>python</category>
      <category>dns</category>
      <category>buildinpublic</category>
    </item>
    <item>
      <title>PDF to Markdown for RAG: per-page text from PDF links, and what to do with scans</title>
      <dc:creator>Mohamed Ayman</dc:creator>
      <pubDate>Tue, 24 Nov 2026 15:10:39 +0000</pubDate>
      <link>https://dev.to/cyrus_1/pdf-to-markdown-for-rag-per-page-text-from-pdf-links-and-what-to-do-with-scans-2lk6</link>
      <guid>https://dev.to/cyrus_1/pdf-to-markdown-for-rag-per-page-text-from-pdf-links-and-what-to-do-with-scans-2lk6</guid>
      <description>&lt;p&gt;Most RAG tutorials start after the text is already extracted. The PDF step is where things go wrong quietly. A scanned page gives you an empty string, your chunker accepts it, and the index fills with blanks.&lt;/p&gt;

&lt;p&gt;This post shows one way to do the extraction step. You send a list of PDF links to an Apify actor, you get one row per file with the text split by page, and you know which pages were scans. I publish the actor on Apify as &lt;code&gt;cyrus_1&lt;/code&gt;, so this is a tutorial from its author. 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;A RAG pipeline needs three things from a PDF:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The text, split so you can chunk by page.&lt;/li&gt;
&lt;li&gt;Some metadata (title, date, page count) to attach to each chunk.&lt;/li&gt;
&lt;li&gt;A way to know when a page has no text, so you do not index nothing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A PDF can be one of two kinds of file. Some have a text layer: the characters are in the file. Others are scans: the file holds a picture of a page. A text parser reads the first kind well and returns nothing for the second. One report can be both, for example a typed contract with a scanned signature page.&lt;/p&gt;

&lt;p&gt;My design choice was to read the text layer first and say plainly which pages have none. Running OCR on every page would be slower and cost more, and most PDFs in a batch already carry their text.&lt;/p&gt;

&lt;h2&gt;
  
  
  The actor
&lt;/h2&gt;

&lt;p&gt;The actor is &lt;a href="https://apify.com/cyrus_1/pdf-to-text-markdown" rel="noopener noreferrer"&gt;PDF to Text and Markdown&lt;/a&gt;. You give it public PDF links. It gives you one JSON row per PDF with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;text&lt;/code&gt; and &lt;code&gt;markdown&lt;/code&gt; for the whole document&lt;/li&gt;
&lt;li&gt;a &lt;code&gt;pages&lt;/code&gt; list with the text of each page&lt;/li&gt;
&lt;li&gt;metadata: title, author, dates, page count, file size&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;needsOcr&lt;/code&gt; and &lt;code&gt;needsOcrPages&lt;/code&gt;, which say which pages had no text layer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is no login, no key for any other service and no proxy. You need an Apify account to run it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Input
&lt;/h2&gt;

&lt;p&gt;This is the whole input for one file. It is the actor's default example, a public IRS form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"urls"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://www.irs.gov/pub/irs-pdf/fw9.pdf"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"includeMarkdown"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"includePages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other ways to give it files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Several links in &lt;code&gt;urls&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Google Drive and Dropbox share links. They must be public ("anyone with the link"). A Drive folder link is refused. A Dropbox folder link is downloaded as a ZIP.&lt;/li&gt;
&lt;li&gt;A ZIP of PDFs.&lt;/li&gt;
&lt;li&gt;A TXT or CSV file that lists links (&lt;code&gt;urlListUrls&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;A web page that links to PDFs (&lt;code&gt;followPdfLinks: true&lt;/code&gt;). It reads that page for links ending in &lt;code&gt;.pdf&lt;/code&gt;, one level deep.&lt;/li&gt;
&lt;li&gt;An Apify dataset with a link field, or files in a key-value store.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It does not take a private folder or a link that needs a login. Those come back as error rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;

&lt;p&gt;This is one row from a real run on that IRS form. I cut the text and kept page 1 of 6:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"rowType"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"document"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://www.irs.gov/pub/irs-pdf/fw9.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"ok"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Form W-9 (Rev. March 2024)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"pageCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"pagesProcessed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"needsOcr"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"needsOcrPages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"charCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;38049&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Form  W-9\n\n(Rev. March 2024)\nDepartment of the Treasury …"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"markdown"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Form  W-9\n\n(Rev. March 2024) Department of the Treasury …"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Form  W-9\n\n(Rev. March 2024)\nDepartment of the Treasury …"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"charCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5778&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"needsOcr"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"charges"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"pdf"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A file that fails gives a row with &lt;code&gt;rowType: "error"&lt;/code&gt;, an &lt;code&gt;errorCode&lt;/code&gt; such as &lt;code&gt;http_error&lt;/code&gt;, &lt;code&gt;too_large&lt;/code&gt;, &lt;code&gt;corrupt_pdf&lt;/code&gt; or &lt;code&gt;encrypted&lt;/code&gt;, and a plain message. Error rows are free. One bad link does not stop the rest of the list.&lt;/p&gt;

&lt;p&gt;A word on the Markdown. It is a best-effort guess. Headings come from font size, lists from bullet characters, paragraphs from line joins. Tables come back as lines of text, not Markdown tables. Pages with several columns are read in the order the PDF library finds them. The &lt;code&gt;markdown&lt;/code&gt; field in the sample above is mostly flowing text. If your documents are table-heavy, test a few before you commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Call it from Python
&lt;/h2&gt;

&lt;p&gt;Install the client with &lt;code&gt;pip install "apify-client&amp;gt;=3.5"&lt;/code&gt;. Put your token in an environment variable, not in the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;apify_client&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;ApifyClient&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ApifyClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"APIFY_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cyrus_1/pdf-to-text-markdown"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;run_input&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"urls"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://www.irs.gov/pub/irs-pdf/fw9.pdf"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default_dataset_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;iterate_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rowType"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"failed:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"errorCode"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"pageCount"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"needs OCR:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"needsOcr"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the IRS form this prints the title &lt;code&gt;Form W-9 (Rev. March 2024)&lt;/code&gt;, &lt;code&gt;6&lt;/code&gt;, and &lt;code&gt;needs OCR: False&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chunk by page for RAG
&lt;/h2&gt;

&lt;p&gt;This part is plain Python and does not depend on the actor. It turns each page into one chunk with metadata and skips pages that have no text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;page_chunks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""Yield one dict per page that has text."""&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"pages"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;  &lt;span class="c1"&gt;# a scan with no text layer: do not index an empty string&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"text"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"page"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default_dataset_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;iterate_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"rowType"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"document"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;page_chunks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"chunks"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here you embed &lt;code&gt;chunks&lt;/code&gt; with whatever model you use. The &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;page&lt;/code&gt; fields let you cite the page in an answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  The same call from JavaScript
&lt;/h2&gt;

&lt;p&gt;Save it as &lt;code&gt;run.mjs&lt;/code&gt; (it uses &lt;code&gt;import&lt;/code&gt; and top-level &lt;code&gt;await&lt;/code&gt;) and run it with &lt;code&gt;node run.mjs&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;ApifyClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"apify-client"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApifyClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;APIFY_TOKEN&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;actor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cyrus_1/pdf-to-text-markdown"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://www.irs.gov/pub/irs-pdf/fw9.pdf"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;defaultDatasetId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listItems&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rowType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errorCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pageCount&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What it costs per 1,000 PDFs
&lt;/h2&gt;

&lt;p&gt;Prices from the actor's README:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A PDF with a text layer, up to 20 pages&lt;/td&gt;
&lt;td&gt;$4 per 1,000 PDFs ($0.004 each)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Each page past 20 in the same PDF&lt;/td&gt;
&lt;td&gt;$0.30 per 1,000 pages&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A PDF with no text layer (OCR off), a failed download, any error row&lt;/td&gt;
&lt;td&gt;free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Apify run-start charge&lt;/td&gt;
&lt;td&gt;very small, shown on the Pricing tab&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Worked examples from the README:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1,000 PDFs of 20 pages or fewer: $4.00.&lt;/li&gt;
&lt;li&gt;One 200-page report: $0.004 + 180 x $0.0003 = $0.058. So $5 covers about 86 of those.&lt;/li&gt;
&lt;li&gt;Apify's free plan includes $5 of credit a month. That is about 1,250 PDFs of up to 20 pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To cap spending, set a maximum cost per run in the run options. The actor stops cleanly when it reaches it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What about scanned pages?
&lt;/h2&gt;

&lt;p&gt;By default the actor reads only the text layer. A page with no text comes back with &lt;code&gt;needsOcr: true&lt;/code&gt;, and a PDF that is all scans is free. That is on purpose: you find out which files are scans before you spend money on them.&lt;/p&gt;

&lt;p&gt;If you want those pages read, set &lt;code&gt;ocrScannedPages&lt;/code&gt; to true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"urls"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://example.com/your-scanned-contract.pdf"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"ocrScannedPages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"ocrLanguage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What the option does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The scanned pages are read by OCR in the same run and merged into the same row, in page order. There is no second actor to call.&lt;/li&gt;
&lt;li&gt;It costs $4 per 1,000 scanned pages where OCR finds text. A page where OCR finds nothing, or fails, is free.&lt;/li&gt;
&lt;li&gt;A PDF made only of scans is charged for its OCR pages and not for the &lt;code&gt;pdf&lt;/code&gt; event on top. A 5-page scan costs $0.020. A 10-page scanned contract costs $0.040, so $5 covers 125 of them.&lt;/li&gt;
&lt;li&gt;A mixed PDF pays the normal price for its text pages, plus each OCR page.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ocrMaxPagesPerPdf&lt;/code&gt; (default 20) caps how many scanned pages of one file are read, so one huge scan cannot surprise you.&lt;/li&gt;
&lt;li&gt;It needs 1024 MB of memory. With less, the run stops at the start with a clear message.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ocrLanguage&lt;/code&gt; can be &lt;code&gt;en&lt;/code&gt;, &lt;code&gt;ch&lt;/code&gt;, &lt;code&gt;korean&lt;/code&gt;, &lt;code&gt;arabic&lt;/code&gt;, &lt;code&gt;cyrillic&lt;/code&gt;, &lt;code&gt;devanagari&lt;/code&gt;, &lt;code&gt;th&lt;/code&gt;, &lt;code&gt;el&lt;/code&gt;, &lt;code&gt;ta&lt;/code&gt; or &lt;code&gt;te&lt;/code&gt;. For Arabic script the text comes back in reading order.&lt;/li&gt;
&lt;li&gt;Each OCR page carries an &lt;code&gt;ocrConfidence&lt;/code&gt;, so you can drop weak pages before indexing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The limits, as the README states them. OCR is imperfect. Low resolution, handwriting, heavy skew and dense small print cause errors. Tables on scanned pages are not rebuilt. OCR pages come back as plain text, with no headings or lists. I do not show OCR output in this post, so test the option on a few of your own scans and read the results before you rely on it.&lt;/p&gt;

&lt;p&gt;If most of your files are scans or photos, the companion actor &lt;a href="https://apify.com/cyrus_1/image-ocr-text-extractor" rel="noopener noreferrer"&gt;Image OCR&lt;/a&gt; is the better fit. It gives word boxes, accurate mode and Japanese. It also reads PDF pages that already have a text layer straight from that layer, at $1 per 1,000 pages instead of $4, and only runs OCR on the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three things that trip people up
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A scan gives an empty page, not an error.&lt;/strong&gt; The text is an empty string and &lt;code&gt;needsOcr&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;. Filter on that before you chunk, as the code above does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Very long documents make very large rows.&lt;/strong&gt; A 500-page book is a few MB of text, and Apify limits one dataset item to 9 MB. For long documents, set &lt;code&gt;includePages&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Some links are not files.&lt;/strong&gt; A Drive folder link is refused. A Drive link to a very large file can return a virus-scan page instead of the file. Both give a free error row, so check for &lt;code&gt;rowType: "error"&lt;/code&gt; in your loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Public links only. Password-protected PDFs are not opened (&lt;code&gt;errorCode: encrypted&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;50 MB per file and 500 pages per file by default. You can change both.&lt;/li&gt;
&lt;li&gt;No proxy. A site that blocks Apify's servers gives a free error row.&lt;/li&gt;
&lt;li&gt;Nothing is stored after the run except the output dataset in your own account.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;The actor is at &lt;a href="https://apify.com/cyrus_1/pdf-to-text-markdown" rel="noopener noreferrer"&gt;https://apify.com/cyrus_1/pdf-to-text-markdown&lt;/a&gt;. Run it with the default input first. It reads one public IRS form and costs a fraction of a cent.&lt;/p&gt;

&lt;p&gt;One question for you: how do you handle tables and multi-column pages before chunking? The actor returns them as lines of text, and I would like to hear what works for others.&lt;/p&gt;

&lt;p&gt;*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 &lt;code&gt;apify-client&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>rag</category>
      <category>pdf</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
