I came across this question whilst looking to convert a scanned PDF to a text-selectable PDF. I later found pdfsandwich which I have had very good results with and I am surprised isn't featured in detail, in the answers so far.
More information is available here: http://www.tobias-elze.de/pdfsandwich/
It uses the Google-sponsored tesseract optical character recognition library behind the scenes but simplifies the PDF processing and creation steps.
As of December 2020, it is included in the official Ubuntu repositories. To install:
sudo apt update && sudo apt install pdfsandwich
To process a PDF called input.pdf:
pdfsandwich input.pdf
By default, your output will appear as something like input_ocr.pdf
On Ubuntu 20.04, it didn't work initially due to a Ghostscript permissions issue. This can be worked around by adding XML comments (<!-- xxx -->
) around out the following lines in /etc/ImageMagick-6/policy.xml (in my file, these were lines 90 - 95):
<policy domain="coder" rights="none" pattern="PS" />
<policy domain="coder" rights="none" pattern="PS2" />
<policy domain="coder" rights="none" pattern="PS3" />
<policy domain="coder" rights="none" pattern="EPS" />
<policy domain="coder" rights="none" pattern="PDF" />
<policy domain="coder" rights="none" pattern="XPS" />
Reference for this fix: https://www.itechlounge.net/2020/09/web-imagickexception-attempt-to-perform-an-operation-not-allowed-by-the-security-policy-pdf/
To read the documentation:
man pdfsandwich
If your document is not English, you can add the -lang
switch like this:
pdfsandwich -lang xxx input.pdf
where xxx
is the language of the document you need to convert. Note that, if not present, you need to install additional language support using:
sudo apt install tesseract-ocr-xxx