After reading these blog posts:

and going through the snippet below (from this gist) for Linux, I think I found a method to OCR a multi-page PDF and get a PDF in the output that could also work in OS X. Most of the dependencies are available in homebrew (brew install tesseract and brew install imagemagick), except one, hocr2pdf.

I haven't been able to find a port of it for OS X. Is there one available? If not, how can one OCR a multi-page PDF and get the results back again in a multi-page PDF in OS X, using free, open source tools?


# This is a script to transform a PDF containing a scanned book into a searchable PDF.
# Based on previous script and many good tips by Konrad Voelkel:
# http://blog.konradvoelkel.de/2010/01/linux-ocr-and-pdf-problem-solved/
# http://blog.konradvoelkel.de/2013/03/scan-to-pdfa/
# Depends on convert (ImageMagick), pdftk and hocr2pdf (ExactImage).
# $ sudo apt-get install imagemagick pdftk exactimage
# You also need at least one OCR software which can be either tesseract or cuneiform.
# $ sudo apt-get install tesseract-ocr
# $ sudo apt-get install cuneiform
# To install languages into tesseract do (e.g. for Portuguese):
# $ sudo apt-get install tesseract-ocr-por

echo "usage: ./pdfocr.sh document.pdf ocr-sfw split lang author title"
# where ocr-sfw is either tesseract or cuneiform
# split is either 0 (already single-paged) or 1 (2 book-pages per pdf-page)
# lang is a language as in "tesseract --list-langs" or "cuneiform -l".
# and author, title are used for the PDF metadata.
# usage example:
# ./pdfocr.sh SomeFile.pdf tesseract 1 por "Some Author" "Some Title"
pdftk "$1" burst dont_ask
for f in pg_*.pdf
if [ "1" == "$3" ]; then
convert -normalize -density 300 -depth 8 -crop 50%x100% +repage $f "$f.png"
convert -normalize -density 300 -depth 8 $f "$f.png"
rm pg_*.pdf

for f in pg_*.png
if [ "tesseract" == "$2" ]; then
tesseract -l $4 -psm 1 $f $f hocr
elif [ "cuneiform" == "$2" ]; then
cuneiform -l $4 -f hocr -o "$f.html" $f
echo "$2 is not a valid OCR software."
hocr2pdf -i $f -r 300 -s -o "$f.pdf" < "$f.html"

pdftk pg_*.pdf cat output merged.pdf

pdftk merged.pdf update_info_utf8 doc_data.txt output merged+data.pdf
echo "InfoBegin" > in.info
echo "InfoKey: Author" >> in.info
echo "InfoValue: $5" >> in.info
echo "InfoBegin" >> in.info
echo "InfoKey: Title" >> in.info
echo "InfoValue: $6" >> in.info
echo "InfoBegin" >> in.info
echo "InfoKey: Creator" >> in.info
echo "InfoValue: PDF OCR scan script" >> in.info
pdftk merged+data.pdf update_info_utf8 in.info output "$in_filename-ocr.pdf"

rm -r doc_data.txt in.info merged* pg_*

2 Answers 2


Tesseract 3.03+ has built in support for PDF output. Which requires leptonica to be installed. You can use: brew install tesseract --HEAD to get the latest version of tesseract. You will also need ghostscript installed but no need for hocr2pdf.

The following script uses ghostscript to split the PDF into JPEGs, tesseract to OCR the JPEGs and output single PDF pages, and finally ghostscript again to combine the pages back into one PDF.


echo Will create a searchable PDF for $y

x=`basename "$y"`

mkdir "$name"
cd "$name"

# splitting to individual pages
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r300 -dTextAlphaBits=4 -o out_%04d.jpg -f "$y"

# process each page
for f in $( ls *.jpg ); do
  # extract text
  tesseract -l eng -psm 3 $f ${f%.*} pdf
  rm $f

# combine all pages back to a single file
gs -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="../${name}_searchable.pdf" *.pdf

cd ..
rm -rf "${name}"

# Adapted from: http://www.morethantechnical.com/2013/11/21/creating-a-searchable-pdf-with-opensource-tools-ghostscript-hocr2pdf-and-tesseract-ocr/
# from http://www.ehow.com/how_6874571_merge-pdf-files-ghostscript.html
# bash tut: http://linuxconfig.org/bash-scripting-tutorial
# Linux PDF,OCR: http://blog.konradvoelkel.de/2013/03/scan-to-pdfa/
  • 1
    In Tesseract 4, this needs to be tesseract $f ${f%.*} -l eng --psm 3 pdf. Commented Sep 18, 2018 at 23:16
  • If the original PDF is composed out of a single image per page (for example the output of non-OCR-capable scanning software), using pdfimages instead of Ghostscript for extracting images avoids recompression. E.g.: pdfimages -all file.pdf out
    – Otto G
    Commented Aug 13, 2021 at 10:43

I use tesseract on os x too. Wrote about automating it briefly here.

  • 5
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. Please take a look here too: apple.stackexchange.com/help/deleted-answers
    – bummi
    Commented Feb 12, 2015 at 9:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .