Making Scanned PDFs Searchable with OCRmyPDF and Tesseract: A Practical Guide
Scanned PDFs are everywhere: signed contracts, old invoices, archived reports, a stack of paper someone ran through the office copier. They look like documents, but to a computer each page is just a picture. You can't search them, you can't copy a sentence out of them, and screen readers see nothing at all.
The fix is OCR (optical character recognition): find the text in the image and add an invisible text layer on top of it, so the PDF still looks the same but behaves like a "real" one. This post walks through a small, practical pipeline I like for doing that on the command line, plus the gotchas that tend to bite people the first time.
The tools
- Tesseract: the open-source OCR engine. It does the actual character recognition and supports 100+ languages.
- OCRmyPDF: a Python wrapper that takes a PDF in and gives a searchable PDF out. It handles rasterising pages, calling Tesseract, and stitching the text layer back in so the original images are preserved.
On macOS:
brew install ocrmypdf On Debian/Ubuntu:
sudo apt install ocrmypdf tesseract-ocr-eng Extra languages are separate packages, e.g. tesseract-ocr-deu for German or tesseract-ocr-spa for Spanish. Run tesseract --list-langs to see what you have.
The one-liner
ocrmypdf input.pdf output.pdf That's genuinely most of it. Open output.pdf, press Ctrl+F, and you should be able to find words that were previously just pixels.
Flags that actually matter
The defaults are sensible, but a few options make a big difference on real-world scans.
Language. Tesseract assumes English. If the document is in another language, say so, or accented characters will come out as garbage:
ocrmypdf -l deu input.pdf output.pdf ocrmypdf -l eng+fra input.pdf output.pdf # mixed-language document Deskew and rotate. Phone photos and hand-fed scans are rarely straight. Skewed lines hurt accuracy a lot.
ocrmypdf --deskew --rotate-pages input.pdf output.pdf --rotate-pages fixes pages that were scanned upside down or sideways, which is more common than you'd think in batch scans.
Clean up noise. If the scan has speckles or a grey background:
ocrmypdf --clean input.pdf output.pdf --clean only affects what Tesseract sees; the visible page stays untouched. There's also --clean-final if you want the cleaned image to be what ends up in the output.
Files that already have some text. By default OCRmyPDF refuses to touch a PDF that already contains text, to avoid doubling it up. For mixed documents (some pages digital, some scanned) use:
ocrmypdf --skip-text input.pdf output.pdf That OCRs only the pages without text. If a previous OCR pass was bad and you want to redo it, --redo-ocr replaces the old text layer.
Output size. OCR can make files bigger. --optimize 2 (or 3) recompresses images afterwards:
ocrmypdf --optimize 2 input.pdf output.pdf Level 3 is more aggressive and can visibly soften images, so check the result before using it on anything important.
Batch processing a folder
For a folder of scans, a simple loop is enough:
mkdir -p searchable for f in scans/*.pdf; do ocrmypdf --deskew --rotate-pages --skip-text -l eng "$f" "searchable/$(basename "$f")" \ || echo "FAILED: $f" done OCRmyPDF already uses multiple cores per file, so running files one after another is usually fine. If you have a huge archive, GNU parallel with a small -j value works, but watch your memory.
Checking the result
Don't trust OCR blindly. Two quick checks:
pdftotext output.pdf - | head -50 This dumps the text layer so you can see what was recognised. If you see lots of random symbols, the language setting is probably wrong or the scan resolution is too low.
The other check is simply searching the PDF for a word you know is on page 1 and a word you know is near the end.
Common problems and what causes them
"The text is there but it's full of mistakes." Usually resolution. Tesseract works best around 300 DPI. A 150 DPI scan of small print will never be great. If you control the scanner, rescan at 300 DPI in greyscale.
"Tables come out as a mess." OCR gives you text in reading order, not structure. For tables you need a second step (a table extractor, or converting the searchable PDF to a spreadsheet) and some manual tidying.
"Handwriting isn't recognised." Tesseract is trained on printed text. Neat block capitals sometimes work; cursive mostly won't.
"The file got huge." Use --optimize, and check that you're not accidentally converting a black-and-white scan to full colour somewhere earlier in your pipeline.
"It says the PDF has text already, but I can't select anything." Some scanners add a hidden, empty or broken text layer. --force-ocr rasterises everything and starts fresh. It's a last resort because it re-renders vector content too.
When you don't want to install anything
The command-line route is great when you have a folder of files or want to automate it. For a one-off file on a machine where you can't install software (a work laptop, a library computer, a phone) a browser-based tool is often simpler. I work on HuntPDF, which has a free OCR PDF tool that does the same job in the browser with no sign-up. The general advice above (good resolution, right language, check the output) applies whichever route you choose.
Why this is worth doing
Making scans searchable is one of those small chores with an outsized payoff. Search works across your whole archive, you can copy quotes out of old documents, and the files become usable for people relying on screen readers. It also unlocks everything downstream: once a PDF has a proper text layer, summarising it, translating it or pulling data out of it all get dramatically easier.
If you have a drawer full of scanned PDFs, a single evening with OCRmyPDF and the loop above will probably sort most of it.