Batch OCR a folder of PDF files, tested
1.6x
faster: 4 files at a time, not 1
4 files at once beat 4 cores a file
31 PDFs, 121 scanned pages, OCRmyPDF on 4 Mac cores:
| Files at a time | Cores a file | Seconds |
|---|---|---|
| 4 | 1 | 112.8 |
| 2 | 2 | 117.8 |
| 1 | 4 | 182.0 |
Some steps run once per file, on one core. OCRmyPDF's own docs say it: for many short files, run more files at once with fewer jobs each.
It uses every core by default, so keep files times
--jobs at your core count.
A plain for loop finished 24 of 31
24 of 31 files done by a for loop
29 of 31 files done by the script below
The loop, for f in *.pdf, missed 7 files:
-
SCAN0001.PDF:
*.pdfdid not match .PDF in capitals. - A file in a subfolder, 2025 taxes/Reçu été.pdf.
- 3 files with some text (exit 6, "already has text").
- A locked file and a broken one.
The script: a folder in, a folder out
in=${1%/} out=${2%/}
export J="${J:-1}" MODE="${MODE:---redo-ocr}"
find "$in" -type f -iname '*.pdf' -print0 |
xargs -0 -n 1 -P "${P:-4}" sh -c '
rel=${3#"$1"/}; dst=$2/$rel
mkdir -p "$(dirname "$dst")"
ocrmypdf --jobs "$J" $MODE --sidecar "${dst%.*}.txt" "$3" "$dst" 2>"${dst%.*}.log"
echo "$? $rel" >>"$2/exit-codes.txt"
' sh "$in" "$out"
Save it as batch-ocr.sh, then run
sh batch-ocr.sh scans out. Each good file gets a
searchable PDF, a .txt and a .log, in the same subfolders.
P sets files at a time, J cores a file. Use a new out folder for each run.
Use --redo-ocr for a mixed folder
Folders mix scans, typed PDFs, and scans with a typed line, like a date stamp.
| Option | Scans with a typed line | Typed pages |
|---|---|---|
| --skip-text | 0.0% | 100.0% |
| --redo-ocr | 99.9% | 100.0% |
| --force-ocr | 99.9% | 99.4% |
--skip-text saw the typed line and skipped those pages. --force-ocr swapped the typed text for OCR text and made the output folder 15% larger.
The .txt files skip pages that had text
| Pages | .txt file | pdftotext |
|---|---|---|
| Clean scans | 100.0% | 100.0% |
| Noisy scans | 99.9% | 99.9% |
| Phone photos, tilted | 72.0% | 60.8% |
| Typed pages | 0.0% | 100.0% |
The .txt holds only new OCR text, so typed pages come out blank. For
those, run pdftotext out/file.pdf.
On tilted photos, the PDF's text lost more. --deskew raised it to 66.0% and took 38% longer, in a --skip-text run (it fails with --redo-ocr). More on --sidecar.
Check exit-codes.txt after each run
grep -v '^0 ' out/exit-codes.txt
8 locked.pdf
15 broken.pdf
-
Exit 8, an open password: OCRmyPDF says to remove it first with
qpdf --decrypt. The fix, tested. - Exit 15, a file cut off halfway: no PDF, but a .txt with part of the text.
No shell? Our hosted Actor
Our pdf-ocr Actor on the Apify Store takes a list of PDF links and returns Markdown or text, not searchable PDFs. I did not run it on this folder.
- Scanned pages: $0.006 each. Digital pages: free. Plus a $0.00005 start fee a run at the default 1 GB.
- On Apify's free plan, it stops at 20 pages a run and 50 a month.
Other ways to OCR many files
- A folder of images, not PDFs: Tesseract batch processing.
- NAPS2 and Stirling-PDF also OCR many PDFs, but by default both skip pages with text: NAPS2 OCR, Stirling-PDF OCR.
How I tested it
- The folder: 24 plain scans of 1, 4 or 8 pages (8 files each), SCAN0001.PDF (3 pages), 2025 taxes/Reçu été.pdf (2), typed.pdf (2 typed pages), mixed.pdf (a typed cover and 3 scans), stamped.pdf (3 scans with a typed line), locked.pdf (a password), broken.pdf (the first half of a 4-page file), and notes.txt.
- Pages: passages from 37 random English Wikipedia articles, drawn at 12 pt on US Letter at 300 dpi in Times New Roman, Georgia or Arial, and saved in the PDF as JPEG. Typed pages: printed to PDF by macOS, so they hold real text.
- Each scan is clean, noisy (blur, noise, hard JPEG) or a phone photo (tilt, keystone, uneven light). Same code as my OCR font test.
- OCRmyPDF 17.13.0, Tesseract 5.5.4 (eng, fast), pdftotext 26.03.0, macOS 26.6 on an Apple M5 Max. Every run used 4 cores in all, at background priority.
- Times: the whole folder, wall clock. The speed rows used --skip-text. The script as printed, with --redo-ocr, took 119.5 s. 4 files by 1 core: 114.0 and 111.5 s. 1 file by 4 cores: 169.7 and 194.3 s. Other rows: 1 run.
- Score: letters other than spaces found in order, against the passage drawn on each page. Extra text does not lower a score. The typed line on stamped.pdf is cut from the output before scoring.
What I did not test
- Windows, Linux and Docker. The script needs a shell with find and xargs.
- The pdf-ocr Actor, NAPS2 and Stirling-PDF on this folder. Stirling-PDF's watched folder.
- The GNU parallel line in OCRmyPDF's batch docs. OCRmyPDF's watched folder (watcher.py) is tested in OCRmyPDF in Docker.
- All 18 cores of the Mac, handwriting, other languages.
Sources
- OCRmyPDF: batch processing
- OCRmyPDF on GitHub (MPL-2.0)
- NAPS2 command line
- Stirling-PDF OCR guide
- Sample article: Jinzhou Operation, text under CC BY-SA 4.0, drawn as page images by me
Questions people ask
How do I batch OCR multiple PDF files?
Run OCRmyPDF once per file from find and xargs, 4 files at a time with --jobs 1 each. On our test folder, that was 1.6x faster than 1 file at a time with --jobs 4.
How do I batch OCR PDFs that already have some text?
Use --redo-ocr. In our folder it read 99.9% of the letters on scans with a typed line and kept the typed text. --skip-text read none of those scans.
Is there a free way to batch OCR PDF files?
Yes. OCRmyPDF (MPL-2.0) and the Tesseract engine it runs (Apache-2.0) are free and open source, and our script runs them on a whole folder. NAPS2 (GPL-2.0-or-later) is free too.