43

I have scanned about 80 pages into gray scale pdf (image format). The end size of the file is about 70MB, which is very huge.

Now I am looking for a method to convert the grayscale image-based PDF file into a simple black/white text-based PDF file.

I have done many attempts with gs but with no success (only a few percent recovery). If any expert has some idea, kindly let me know.

5
  • 1
    You need some OCR tool. Look at Tesseract ubuntuforums.org/showthread.php?t=880471
    – Nikita U.
    Mar 14, 2013 at 23:26
  • 4
    If you're interested in keeping the PDF images as-is and adding text to it, have a look at question Adding OCR info to a PDF.
    – colan
    Sep 6, 2014 at 13:22
  • If you could post a link to (for example) one-page example we could test solutions...
    – Rmano
    Apr 30, 2015 at 8:23
  • It's not an OCR solution but askubuntu.com/a/3387/16395 helps a lot (although 72dpi is a bit on the low side, I have better results with 120).
    – Rmano
    Apr 30, 2015 at 8:29
  • Does YAGF work correct with Ubuntu 16.04? If I load an image or a pdf-document the programm aborts without any error message. Under Ubuntu 14.04 I had no problems. H.Roos Nov 5, 2016 at 11:08

8 Answers 8

31

gImageReader is a simple GTK+ front-end to tesseract-ocr.

sudo apt-get install gimagereader tesseract-ocr

sorry for the german text

3
  • 5
    You should also install the document's language to improve the OCR, with sudo apt-get install tesseract-ocr-[lang], replacing lang by the language code, like deu for Deutsch, por for Portuguese, etc.
    – estibordo
    Sep 7, 2018 at 13:53
  • 1
    This software is ugly. Usability is under zero. Though it tries to do the job, but it's unable to read plain spreadsheet-like tables. Just misses the pages containing them.
    – Max Yudin
    Oct 29, 2018 at 9:11
  • This worked well for me. Just drag in the pdf, click the "Recognize all" button at the top. With a one page pdf, I waited just a few seconds, and the result appeared in a new third pane on the right hand side. The plain text in this pane can then be edited if required, and saved by clicking its "Save output" button. You can also use the mouse to highlight a particular region, and right click.
    – user643722
    Dec 21, 2021 at 12:21
10

You can try pdfocr:

 sudo add-apt-repository ppa:gezakovacs/pdfocr
 sudo apt-get update
 sudo apt-get install pdfocr

To execute the syntax is

 pdfocr -i input.pdf -o output.pdf

where input.pdf is the name of the input file and output.pdf the output file.

By default it uses Tesseract. To install it:

 sudo apt-get install tesseract-ocr

pdfocr creates an embedded text layer.

4
  • Great! Interestingly after performing the steps above the file is now searchable in Adobe Acrobat DC but not in Preview.
    – lukeaus
    Aug 1, 2017 at 21:55
  • 2
    This repository doesn't support xenial
    – Max N
    Dec 18, 2017 at 21:19
  • You can try installing an old version of pdfocr, if you install wily version on xenial it works ok. To do this add "deb ppa.launchpad.net/gezakovacs/pdfocr/ubuntu wily main" and "deb-src ppa.launchpad.net/gezakovacs/pdfocr/ubuntu wily main" to /etc/apt/sources.list and then "sudo apt update" and "sudo apt-get install pdfocr"
    – rafmunozf
    Jan 28, 2018 at 21:09
  • 3
    pdfocr is a script which automates the following process: 1. Splitting the PDF file into separate pages using pdftk 2. Extracting out the image data using pdfimages 3. Doing OCR (optical character recognition) using cuneiform 4. Embedding the detected text back into the PDF file using hocr2pdf 5. Merging together the files using pdftk. (quoting from ubuntuforums.org/showthread.php?t=1456756 ) Aug 16, 2018 at 14:01
4

Have a look at OCRmyPDF that works well.

3
pdfsandwich

It loads tesseract and others on install. It's an easy one step solution and can be scripted. It can use hocr2pdf to create a plain text pdf, but its not ready for prime time...yet. The default uses tesseract and creates a "sandwiched" pdf: image + text underneath.

The embedded image can be removed with commands like:

gs -o ocr_noIMG.pdf -sDEVICE=pdfwrite -dFILTERIMAGE ocr_image.pdf

but the text is hidden, so it looks like a blank page.

Loading the PDF into LibreOffice Draw exposes the text and the image can be deleted manually.

1
2

You could try shrinkpdf to reduce the filesize and then ocr.sh to add the text layer.

1

For the graphical interface suggested by @A.B. on ubuntu 14.04 you should follow:

ocr tesseract on ubuntu 14.04

or anyway, add to the repository list:

sudo add-apt-repository ppa:sandromani/gimagereader
sudo apt-get update

before this works:

sudo apt-get install gimagereader
1

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
0

Actually the best I've found is the command pdftotext

sudo apt install poppler-utils

pretty slick and simple if you do pdftotext -layout xxx.pdf you even get the original layout preserved as text.

1
  • recoll uses pdftotext in it's indexing, I know not how or why.
    – pierrely
    Nov 10 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.