Ghostscript OCR, tested on scanned book pages
1,881
of 1,955 words found
Run Ghostscript OCR
23 s for 10 pages, Mac CPU, 2 threads
4.9 MB searchable PDF, from a 6.5 MB scan
On a Mac, in a folder with scan.pdf:
brew install ghostscript
gs -sDEVICE=ocr -r300 -o out.txt scan.pdf
gs -sDEVICE=pdfocr8 -r300 -sCompression=JPEG -o searchable.pdf scan.pdf
The first command writes the text. The second writes a searchable PDF: gray page images with the text under them.
brew install ghostscript also installs Tesseract 5.5.4
and its English data. Ghostscript calls it from inside.
Ghostscript vs Tesseract vs OCRmyPDF
| Tool | Output | Words found |
|---|---|---|
| Ghostscript, ocr | Text | 1,881 |
| Tesseract 5.5.4 | Text | 1,877 |
| Ghostscript, pdfocr8 | 1,875 | |
| OCRmyPDF 17.13 | 1,855 |
Ghostscript runs the same Tesseract, so the counts are close. For the two PDFs, I read the text layer back with pdftotext.
OCRmyPDF kept the scan's color JPEGs. Ghostscript drew new gray pages.
Searchable PDF: add -sCompression=JPEG
| Command | Words | Size |
|---|---|---|
pdfocr8 -r300 |
1,875 | 19.4 MB |
Same, plus -sCompression=JPEG |
1,875 | 4.9 MB |
pdfocr8, no -r |
1,872 | 28.3 MB |
pdfocr24 -r300, color |
1,714 | 50.2 MB |
With no -r, pdfocr8 drew each page at 600 dpi. The color
device found 161 fewer words. The manual spells the value "jpeg". That
stopped at once:
Unknown compression setting
Write JPEG in capitals.
Text: the resolution mattered little
| Switch | Words found | Time |
|---|---|---|
No -r |
1,870 | 10 s |
-r150 |
1,882 | 15 s |
-r200 |
1,880 | 18 s |
-r300 |
1,881 | 23 s |
No -r gave the same text as -r72. These are
clean 300 ppi scans. I use -r300, the scan's own
resolution.
-sUseOCR does not read scans
pdfwrite has an OCR switch. On the scan, it made a PDF with 0 words of text:
gs -sDEVICE=pdfwrite -sUseOCR=Always -o out.pdf scan.pdf
It reads font glyphs that have no Unicode map, not page images. For a scan, use pdfocr8.
Other languages: the TESSDATA_PREFIX error
Homebrew's Tesseract comes with English only. With
-sOCRLanguage=deu, Ghostscript stopped:
Error opening data file /opt/homebrew/share/tessdata/deu.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.
Failed loading language 'deu'
Tesseract couldn't load any languages!
**** Unable to open the initial device, quitting.
Put the traineddata file in a folder, and give that folder in
TESSDATA_PREFIX. I tested that with a renamed copy of eng.traineddata.
For two languages: -sOCRLanguage="eng+deu".
AGPL-3.0
Ghostscript is AGPL-3.0, from Artifex, who also sell a commercial license. Running it on your own files is fine. If you build it into a product or a web service, read the AGPL first.
Need text with nothing to install?
Our Actor takes PDF, JPG and PNG files and returns Markdown, from one web request. On the same 10 pages, inside a PDF, it found 1,874 words.
curl -s -X POST "https://api.apify.com/v2/acts/brasspage~pdf-ocr-to-markdown/run-sync-get-dataset-items" \
-H "Authorization: Bearer $APIFY_TOKEN" \
-H "Content-Type: application/json" \
-d '{"documentUrls": ["https://api.apify.com/v2/key-value-stores/sTnmNrGoSsi7tLQvs/records/anythingllm-mixed-11-pages.pdf"]}' \
| jq -r '.[].markdown' > book.md
It costs $0.006 a page.
How I tested it
- Pages: the 10 scans of OCR in n8n, proofread Wikisource book pages, 1,955 true words. Each page is one color JPEG, 1,920 x 3,079 pixels, at 300 ppi.
-
Ghostscript 10.08.0 from Homebrew, with Tesseract 5.5.4 and its
English data, on an Apple Silicon Mac. 2 threads
(
OMP_THREAD_LIMIT=2), lowest priority, other jobs on the Mac. One run each. - Tesseract 5.5.4 alone: the CLI on the 10 JPEGs, English, default settings.
-
OCRmyPDF 17.13.0:
ocrmypdf -j 2 scan.pdf out.pdf, with no Ghostscript on the PATH. Its output was PDF/A-2b. - Score: true words found in order, with the scorer of the tessdata test. PDFs were read back with pdftotext from poppler.
- The Actor: one real run, build 0.1.65, on November 12. Its file holds the same 10 pages plus a typed cover page.
What I did not test
- The hocr and pdfocr32 devices, and JPEG quality other than the default.
- OCRmyPDF with Ghostscript on the PATH.
- A real German page, and Homebrew's tesseract-lang.
- Windows and Linux builds of Ghostscript.
Sources
Two ways to skip the work
Run it yourself. Our Apify Actor turns scanned PDFs into Markdown, tables included.
Or send us the scan. Tack, our AI, does the job for $29, up to 50 pages. First page free.
Questions people ask
Can Ghostscript OCR a scanned PDF?
Yes. The ocr device writes the text, and the pdfocr8 device writes a searchable PDF. On 10 scanned pages, Ghostscript 10.08.0 found 1,881 of 1,955 words.
Is Ghostscript OCR better than OCRmyPDF?
Close on text: the pdfocr8 text layer had 1,875 words and OCRmyPDF had 1,855. OCRmyPDF kept the original color images. Ghostscript made new gray ones.
Why is my Ghostscript OCR PDF so big?
pdfocr8 draws each page again as a new image. From a 6.5 MB scan it made 19.4 MB. With -sCompression=JPEG, in capitals, it made 4.9 MB with the same text.