# Changelog of PDF to Text & OCR - Scanned PDFs, Images, Word, Japanese (`quietworkslab/pdf-text-extractor`) Actor

- **URL**: https://apify.com/quietworkslab/pdf-text-extractor/changelog.md
- **Full Actor documentation**: https://apify.com/quietworkslab/pdf-text-extractor.md

## Changelog

### 0.2 (2026-11-21, AI model)

- The OCR model is now Google Gemini 3.1 Flash-Lite (`google/gemini-3.1-flash-lite`). Since about 2026-11-20, Apify's OpenRouter account allows only zero-data-retention endpoints and none is left for Gemini 2.5 Flash-Lite: every OCR request failed with `AI_MODEL_UNAVAILABLE` (HTTP 404) and the run stopped without charges. Requests now also list `google/gemini-3.5-flash-lite` as a fallback, which OpenRouter uses only when the first model cannot answer; `OUTPUT.aiUsage` has `fallbackModels` and `modelsUsed` (answers per model).
- One more fixed sentence in the OCR instructions: keep the final period of every line and sentence, also in lists (the new model left it out on similar lines).
- Measured on Apify (build 0.2.9): the test scan's English, Japanese, vertical Japanese and table pages and the text-layer page without errors, the 1,080 x 6,000 screenshot at a character error rate of 0.6%, the three screenshots with all lines; the AI cost is about $0.0005-0.002 per page on paid plans (README updated).

### 0.2 (not published yet)

- **OCR for scanned PDFs and images** (new input `ocr`, default `auto`): the pages of a PDF without a usable text layer (all pages of a scan, `NO_TEXT_LAYER`, or of a PDF whose characters are all unreadable, `GARBLED_TEXT`; or the empty pages of a PDF where half or more of the pages have no text, the others keeping their text layer) and PNG, JPEG and WebP images are read by the AI model Google Gemini 2.5 Flash-Lite (`google/gemini-2.5-flash-lite`) through Apify's official `apify/openrouter` Actor, with the run's own token. `always` reads every PDF page by OCR; `off` is the 0.1 behaviour and sends nothing to any AI. DOCX files are never OCR'd. A PDF whose text only looks garbled (`garbledSuspected`) keeps its text as before (not charged) and its row suggests `always`.

- Pages are drawn with pdf.js and `@napi-rs/canvas` (new dependency, 1.1.1) in a second worker thread under the same memory watchdog, about 2,000 px on the long side, with pdf.js's WebAssembly decoders for JBIG2, CCITT fax and JPEG 2000 scans (without them such scans drew as blank pages without an error). A page or image taller than 1:1.45 is cut into parts of at most 1:1.41 at the lightest band between lines; each part is one OCR page. Pages with almost no ink are not sent and not charged. Up to 4 requests at a time; results in page order, merged with the text-layer pages. Plain-text answers with fixed instructions (reading order, paragraphs, no spaces between Japanese characters, tategaki, tables as Markdown or tab-separated cells); a comment instead of the text is asked for once more and then fails the page; an answer cut by the 2,048-token limit is redone in halves (depth 2). 429 is retried 4 times, server errors twice; 401, 402, 403 and 404 stop the run.

- Images as input from links, uploads and the key-value store (with OCR off, images in a store are skipped as before). Images over 25 MB or 60 megapixels (read from the header before decoding) and TIFF, HEIC, AVIF, GIF and BMP fail with a reason and are not charged. Link downloads now accept image types (the first bytes decide; SVG is refused).

- New event `page-ocr`: one per OCR page whose OCR gave text. `document-extracted` now counts only the pages whose text came from the text layer. Blank pages, failed pages and pages left by a limit are free. Before a page is sent to the AI, the rest of the maximum cost per run must cover one more `page-ocr` for it (on top of the pages in flight); otherwise OCR stops there and the run ends `PARTIAL`. Both events are charged after the rows and the checkpoint; the Actor's own counts (an upper bound saved before each charge, and the count the SDK confirmed) carry over a migration or a resurrect, as in Audio & Video to Text.

- AI spend caps (the maximum cost per run does not limit the AI bill): `maxOcrPages` per run, empty = 200 on a paid Apify plan and 20 on the Free plan (`APIFY_USER_IS_PAYING`, or unknown on Apify); every request, retries and halves included, counts towards a hard limit of twice that, written ahead to the run's store (`OCR-SENT`) before the request goes out. When a cap is reached, the rest of the run reads text layers only and the rows say how many pages were left and why.

- Resume per OCR page: the pages read for the file in work are saved (`OCR-000001` ...), so a moved, killed or resurrected run does not send them to the AI again; the record is removed after the file. "aborting" and the timeout keep the pages read (charged for them); "migrating" abandons the file without a row.

- Rows: new fields `textSource`, `ocrPages`, `ocrBlankPages`, `ocrFailedPages`, `ocrPagesLeft`, `ocrNote`, `ocrPrepared`, `chargedOcrPages`; `format` can be `png`, `jpeg` or `webp`. OUTPUT: `chargedOcrEvents`, `ocr` counts, `aiUsage` (requests, tokens, the cost OpenRouter reported), `sampleOcrPrepared`, `selfTest`, and the OCR settings.

- A third built-in sample: a scanned page (English and Japanese, written for this Actor, `scripts/make-ocr-sample.js`). The default run draws it like any scan and shows its known text as a prepared OCR result: no AI call, no charge. Input `{"selfTest": true}` (not in the form) sends it to the real AI and writes the character error rate to `OUTPUT.selfTest` (no charges of this Actor).

- Dockerfile: optional packages are installed again (`--include=optional`) for the canvas binary of Alpine (musl); the build check now draws the scanned sample and three check scans (CCITT, JBIG2, JPEG 2000 in `sample/check`, `scripts/make-scan-fixtures.py`) and decodes PNG, JPEG and WebP, and fails the build if it cannot.

- After the live check of build 0.2.1 and the review (2026-11-14, REVIEW-5):
  - OCR text follows `lineJoin`: the AI is asked for one line per paragraph (`smart`) or the page's line breaks (`keep`), and with `smart` the lines it still leaves broken inside a paragraph are joined as in the text layer (table rows, list items and headings never). The AI is also told to copy every character and punctuation mark exactly and never to change a word (live, it dropped the final period of many lines and once wrote another word). The self-test compares the row's text in the joined form (`characterErrorRate`; `characterErrorRateNoSpaces` without spaces and line breaks).
  - A fixed answer format: the answer must start with the line `[[OCR]]`; anything before it is dropped, an answer without it is asked for once more and then fails the page (`AI_BAD_FORMAT`, not charged), and `[[NO_TEXT]]` after it means no text (not charged). Real text that starts like a comment ("I cannot attend ...", "この画像の無断転載を禁じます。") is kept; a line such as "Here is the transcription:" is not.
  - The blank check counts ink both ways around the most common brightness: dark-mode screenshots, code editors, terminals and dark slides were taken for blank. Transparent images are drawn on grey (white text on a transparent PNG was lost). Cut points use the same rule.
  - Sections by height: an image is scaled down only when wider than 2,000 px, and up to 2,800 px high it is one OCR page (phone screenshots, US Legal pages: one, not two); a taller one is cut into parts of at most 2,800 px. The width is never made smaller to have fewer parts (a 800 x 75,000 image was drawn 299 px wide); past 40 parts per page or image, the rest is left (not charged) and the row says so.
  - Drawing pages that turn out blank or cannot be read counts as unpaid run time inside the file, so a long PDF of slow blank pages stops at the limit instead of running until the timeout.
  - A page that needs more OCR pages than `maxOcrPages` has left is left alone; it no longer closes OCR for the rest of the run. The start of every page is written ahead to `OCR-SENT`, so a restarted run counts it against `maxOcrPages`.
  - Smaller: with one row per page, a page that OCR could not read (or a blank one) says so in its own row; a half that fails keeps the text of the other half; sections that failed are not saved for a restart (a passing AI error is tried again); the log no longer shows the start of an answer that was not used; the memory a failed page recommends is 2 GB (scans of 600 dpi need about 585 MB more).
  - After the live reboot check of build 0.2.2 (run NjX78YrLHoQGBzGVh): `maxOcrPages` counts each section of a file once, by key; the keys counted for the file in work are written ahead to `OCR-SENT` with the count, so a page that was in flight (or read but not saved yet) when the run was moved is started again without counting a second time (5 pages counted twice and 8 of 20 pages were left). The request block written ahead is 2 instead of 8, so a restart counts at most a few unsent requests against the hard limit.
  - Store title and description (`actor.json`): "PDF & Image OCR to Text - Scanned PDF, Word, Japanese", with the OCR price. README: the title, sections by height, the answer format, the measured memory per scan resolution, the first OCR numbers measured on Apify, and that a cut is at the lightest band (not "never through a line").

### 0.1 (not published yet)

- First version: extract the text of PDF and Word (DOCX) files given as direct file links (up to 100 per run), in a key-value store (optionally only keys with a prefix) or uploaded in the input form. The format is detected from the file's first bytes.
- PDF: the text layer, read with pdf.js 6.5 (no OCR). The pieces of a line are joined without spaces between kanji, kana and Japanese punctuation; vertical Japanese text is read column by column, right to left; wrapped lines are joined into paragraphs, and an English word hyphenated at a line end is mended (`lineJoin: smart`), or the PDF's line breaks are kept (`keep`). Lines that are only a page number, and lines that repeat at the top or bottom of at least half of the pages (numbers ignored), are removed (`removeHeadersFooters`). Optional page ranges (`pages`).
- DOCX: paragraphs and tables of the main document in order, text boxes once, tracked deletions and field codes left out. Headers, footers, footnotes and comments are not read.
- One row per document or per PDF page (`splitBy`), plain text or simple Markdown (`outputFormat`: headings from the font size of a PDF or the styles of a DOCX, lists, DOCX tables). Character clean-up `basic`, `japanese` (the defaults of Japanese Text Normalizer) or `none`. Text longer than 7 MB goes on in further rows (`part`).
- Scanned PDFs (half or more of the pages without text) and garbled text (mostly private-use or replacement characters) are written as `no-text` with `needsOcr: true` and are not charged. Password-protected, damaged and unsupported files fail with a reason and are not charged.
- Pay-per-event: `document-extracted`, one per started 20 PDF pages read or per started 50,000 DOCX characters; the built-in samples are not charged. Each document is charged right after its rows and the progress are saved; a document whose charge reaches the maximum cost per run writes the final summary first; a PDF whose pages the rest of the limit does not cover is not read. Stops cleanly on abort, migration (goes on after the last saved file) and 30 seconds before the run's timeout.
- Safety: pdf.js without font rendering, wasm decoders or scripts, with its bundled cMaps and standard fonts; every file is read in a worker thread with a heap limit, ended after 120 seconds, a stop signal or a crash. DOCX files are unpacked with node:zlib under a 200 MB and 10,000-entry limit, with CRC checks; XML with a document type declaration is refused. Links: one GET per link, up to 5 redirects, private and local addresses refused (also after redirects), web pages refused by Content-Type and first bytes, 100 MB and 60 seconds per link, no cookies or logins.
- After the independent review (2026-11-13, REVIEW-1): a memory watchdog ends the reader when the process uses more than 70% of the run's memory (a 1 MB PDF that unpacks to 1 GB took the process to 2.2 GB before; the worker's heap limit does not cover pdf.js's decoded streams), and the worker's heap is now half of what is left after 256 MB; such a file fails with `TOO_LARGE_FOR_MEMORY`, is not charged, and the run goes on. A DOCX part is stopped at the size its ZIP directory declares (a part that declared 1 KB and unpacked to 190 MB passed the memory check and ended the run), the memory check counts exactly the parts that are read, and one file may give at most an eighth of the main thread's heap in characters (about 33 million at 1 GB). A run that failed after charging and is resurrected goes on after the last saved file instead of starting again (it charged the first documents twice); a resurrected run whose final summary was saved fixes `chargedEvents` in OUTPUT and ends failed again when that summary said FAILED. PDFs are charged for their pages with text: a document where half or more of the pages have none is `ok` with `needsOcr: true`, and only one with fewer than 10% of its pages with text is `no-text` (blank pages no longer make a document free). Garbled text is also found when a font without a Unicode map gives glyph numbers as characters (few letters and digits, or lines that mix more than 3 alphabets): `GARBLED_TEXT`, not charged. Headers and footers keep content when in doubt: a bare number counts as a page number only when it goes up with the page, a repeated line with numbers only when those numbers go up with the page, and lines in a bigger font than the body and sentences are never removed (years, subtotals and numbered headings were lost). Smaller fixes: a self-closing element in a skipped DOCX range no longer ends it (text boxes came twice); Node.js error codes no longer reach a row (`EXTRACTION_FAILED`, damaged ZIP fields are `CORRUPT_FILE`); the XML scan is linear also on unclosed tags and deep nesting; `basic` turns vertical punctuation forms (︒ ︑) into 。、; rows are sent in batches of up to 8 MB; the built-in samples respect a priced dataset row. README: pricing by pages with text, the new header rules, memory for text-heavy PDFs, what OUTPUT holds of failed links.
- After the second independent review (2026-11-13, REVIEW-2): the memory of a run is split by one function (watchdog 70%, the reader's heap, the process heap flag never below it, now set by `src/heap-flag.js` in the Dockerfile; with the flag below the reader's heap, one large allocation in the reader ended the whole process). A DOCX whose parts cannot be planned (relationships over 4 MB) counts every part's declared size in the memory check, and the reader refuses more XML than a quarter of its heap before unpacking. Before a file is refused for memory, the reader is ended and the memory measured again; the recommended memory is always more than the run has, or the message says the file is too large even for 4 GB. Garbled text: only PDFs are judged, combining marks count as letters, repeated symbols and dot leaders count once, symbols only inside dense words, mixed scripts only inside one word, and a document only when most of its pages look garbled; such a document keeps its text, is `ok` with `needsOcr` and the new field `garbledSuspected`, and is not charged (forms, tables of contents, tables, formulas, glossaries, Burmese and other scripts were taken for garbled before). A PDF is `no-text` only when it has fewer than 10 meaningful characters in all; otherwise it is charged for its pages with text (blank pages no longer make text free). A line that is exactly the same at the top or bottom of at least half of the pages (and 3) is removed even when it ends with a full stop or is set bigger; a 2-page excerpt whose page numbers share an offset loses them. A worker that ends on its own after a file is not used again; uploads that cannot be read stay in the progress for a resurrected run. README and the input schema describe the new header rules and pricing.
- After the third independent review (2026-11-13, REVIEW-3): garbled text is also found when the characters first seen in a PDF run in code-point order (a subset font without a Unicode map numbers its glyphs in the order they are used; 7 of 8 such test PDFs were charged before); the README calls the detection best-effort. A line that is exactly the same at the top or bottom of at least half of the pages now stays on the first page where it appears (table headings, a mail merge's first greeting and letterheads were removed from every page), and a head that differs between left and right pages counts against the pages of its side. The memory watchdog stops the reader at 60% of the run's memory and looks every 10 ms (the reader overshot 70% to about 90% in a 1 GB run); the memory check still admits files up to 70%. A short scan whose every page has the same single line (a scanner's watermark) is `no-text`. The sample PDF says that the header stays on its first page. Schemas, README and the Store description: 10 characters, "per 20 pages with text", the DOCX reader's memory limit, the Pricing tab's event name.
- After the fourth independent review (2026-11-13, REVIEW-4): lines that repeat exactly at the top or bottom of the pages, including the copy kept on the first page, no longer count as text for the no-text rule or the pages billed, also when headers and footers are kept. A scan of 3 or more pages with a scanning app's watermark on every page is `no-text` and free again (it was `ok` and charged one event after REVIEW-3); a page whose only text is the running header is a page without text.
- After the checks on Apify (2026-11-13, build 0.1.5): after a migration the SDK starts from the run's charged-event count, which can lag a few seconds behind (a rebooted run reported 16 of its 20 events). The Actor now keeps its own count in OUTPUT (`resume.chargedEvents`) and, when it continues, uses the larger of that count and the SDK's for the maximum cost per run and for `chargedEvents`. README: the Limits table now has the times and memory measured on Apify (1 GB, 512 MB and 2 GB) and the drawings that read or stopped at 1 GB.
- After the re-run of that check on Apify (build 0.1.6): a run restarted by a reboot logged "Continuing after a migration" and then waited, without a word, until the run's timeout. The saved count is not the cause: the whole Actor continuing locally from a saved OUTPUT, with the SDK count far behind, finishes in about 2 seconds (new test). Every call to the platform in the loop now has a time limit (120 s, and never past 10 s before the run's timeout) and says every 30 s which step it is waiting for: a record that does not answer fails with `READ_FAILED` and the run goes on; the list of keys, the summary, the rows or a charge that do not answer end the run with an error naming the step; reading the list or a record also ends at once on a stop signal. An ended reader worker is waited for at most 5 seconds.
- README only (2026-11-14): "More from Quiet Works Lab" also lists Audio & Video to Text (transcripts and SRT or WebVTT subtitles from audio and video files). No input field, price or event changed.
