49

How do I extract text from a PDF that wasn't built with an index? It's all text, but I can't search or select anything. I'm running Kubuntu, and Okular doesn't have this feature.

1

11 Answers 11

26

I have had success with the BSD-licensed Linux port of Cuneiform OCR system.

No binary packages seem to be available, so you need to build it from source. Be sure to have the ImageMagick C++ libraries installed to have support for essentially any input image format (otherwise it will only accept BMP).

While it appears to be essentially undocumented apart from a brief README file, I've found the OCR results quite good. The nice thing about it is that it can output position information for the OCR text in hOCR format, so that it becomes possible to put the text back in in the correct position in a hidden layer of a PDF file. This way you can create "searchable" PDFs from which you can copy text.

I have used hocr2pdf to recreate PDFs out of the original image-only PDFs and OCR results. Sadly, the program does not appear to support creating multi-page PDFs, so you might have to create a script to handle them:

#!/bin/bash
# Run OCR on a multi-page PDF file and create a new pdf with the
# extracted text in hidden layer. Requires cuneiform, hocr2pdf, gs.
# Usage: ./dwim.sh input.pdf output.pdf

set -e

input="$1"
output="$2"

tmpdir="$(mktemp -d)"

# extract images of the pages (note: resolution hard-coded)
gs -SDEVICE=tiffg4 -r300x300 -sOutputFile="$tmpdir/page-%04d.tiff" -dNOPAUSE -dBATCH -- "$input"

# OCR each page individually and convert into PDF
for page in "$tmpdir"/page-*.tiff
do
    base="${page%.tiff}"
    cuneiform -f hocr -o "$base.html" "$page"
    hocr2pdf -i "$page" -o "$base.pdf" < "$base.html"
done

# combine the pages into one PDF
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$output" "$tmpdir"/page-*.pdf

rm -rf -- "$tmpdir"

Please note that the above script is very rudimentary. For example, it does not retain any PDF metadata.

8
  • Any idea to improve this script to add spell-checking stage to correct errors in recognition step? Jun 21, 2011 at 21:49
  • @Gökhan Sever, do you mean adding interactive spell-checking where the user is prompted for replacement for misspelled/unknown words? I think you could do that by adding something like aspell check --mode=html "$base.html" in the script right after running cuneiform. Jun 21, 2011 at 22:48
  • This is one solution. However without seeing the whole context of the text it is hard to make corrections. It would be nicer to see an interface built within the ocrfeeder. Jun 22, 2011 at 0:22
  • 1
    By the way, I use tesseract for character recognition: replacing cuneiform line with: tesseract "$page" "$base" hocr Jun 22, 2011 at 0:22
  • 1
    Small correction: The line for tesseract at least for other languages than English, here e.g. German ( = deu ) is: ` tesseract "$page" "$base" -l deu hocr ` (of course you have to remove the ` `).
    – Keks Dose
    Oct 12, 2012 at 15:45
15

See if pdftotext will work for you. If it's not on your machine, you'll have to install the poppler-utils package

sudo apt-get install poppler-utils 

You might also find the pdf toolkit of use.

A full list of pdf software here on wikipedia.

Edit: Since you do need OCR capabilities, I think you'll have to try a different tack. (i.e I couldn't find a linux pdf2text converter that does OCR).

  • Convert the pdf to an image
  • Scan the image to text using OCR tools

Convert pdf to image

  • gs: The below command should convert multipage pdf to individual tiff files.

    gs -SDEVICE=tiffg4 -r600x600 -sPAPERSIZE=letter -sOutputFile=filename_%04d.tif -dNOPAUSE -dBATCH -- filename

  • ImageMagik utilities: There are other questions on the SuperUser site about using ImageMagik that you might use to help you do the conversion.

    convert foo.pdf foo.png

Convert image to text with OCR

Taken from the Wikipedia's list of OCR software

4
  • 2
    Does this program also work for handwritten text documents?
    – Ivo Flipse
    Aug 24, 2009 at 9:31
  • 1
    No, I don't think it has OCR capabilities. It can just extract the text embedded in the pdf. Man page: linux.die.net/man/1/pdftotext
    – user4358
    Aug 24, 2009 at 10:50
  • Yeah, this works for pdf documents that already come with the text embedded. My case is exactly one where it doesn't. Aug 27, 2009 at 3:28
  • 1
    @obvio171 Added the best option I could find for getting OCR to work in your case.
    – user4358
    Aug 27, 2009 at 6:53
14

Google docs will now use OCR to convert your uploaded image/pdf documents to text. I have had good success with it.

They are using the OCR system that is used for the gigantic Google Books project.

However, it must be noted that only PDFs to a size of 2 MB will be accepted for processing.

Update
1. To try it out, upload a <2MB pdf to google docs from a web browser.
2. Right click on the uploaded document and click "Open with Google Docs".
...Google Docs will convert to text and output to a new file with same name but Google Docs type in same folder.

3
  • The answer is not really Ubuntu-specific but I want to really thank you: BRILLIANT solution! :)
    – Pitto
    Mar 28, 2012 at 16:34
  • This was really helpful :) I uploaded a 50 MB file yesterday and it worked. Looks like they've increased the size limit.
    – Gaurav
    Apr 20, 2018 at 20:27
  • Yes, they will OCR your PDF and keep a copy of both the PDF and the text for their own use (along with metadata tying it to you, of course) Sep 7, 2023 at 14:32
7

Best and easyest way out there is to use pypdfocr it doesn't change the pdf

pypdfocr your_document.pdf

At the end you will have another your_document_ocr.pdf the way you want it with searchable text. The app doesn't change the quality of the image. Increases the size of the file a bit by adding the overlay text.

Update 3rd november 2018:

pypdfocr is no longer supported since 2016 and I noticed some problems due to not being mentained. ocrmypdf(module) does a symiliar job and can be used like this:

ocrmypdf in.pdf out.pdf

To install:

pip install ocrmypdf

or

apt install ocrmypdf
3

PDFBeads works well for me. This thread “Convert Scanned Images to a Single PDF File” got me up and running. For a b&w book scan, you need to:

  1. Create an image for every page of the PDF; either of the gs examples above should work
  2. Generate hOCR output for each page; I used tesseract (but note that Cuneiform seems to work better).
  3. Move the images and the hOCR files to a new folder; the filenames must correspond, so file001.tif needs file001.html, file002.tif file002.html, etc.
  4. In the new folder, run

    pdfbeads * > ../Output.pdf
    

This will put the collated, OCR'd PDF in the parent directory.

3

Geza Kovacs has made an Ubuntu package that is basically a script using hocr2pdf as Jukka suggested, but makes things a bit faster to setup.

From Geza's Ubuntu forum post with details on the package...

Adding the repository and installing in Ubuntu

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

Running ocr on a file

pdfocr -i input.pdf -o output.pdf

GitHub repository for the code https://github.com/gkovacs/pdfocr/

2

another script using tesseract :

#!/bin/bash
# Run OCR on a multi-page PDF file and create a txt with the
# extracted text in hidden layer. Requires tesseract, gs.
# Usage: ./pdf2ocr.sh input.pdf output.txt

set -e

input="$1"
output="$2"

tmpdir="$(mktemp -d)"

# extract images of the pages (note: resolution hard-coded)
gs -SDEVICE=tiff24nc -r300x300 -sOutputFile="$tmpdir/page-%04d.tiff" -dNOPAUSE -dBATCH -- "$input"

# OCR each page individually and convert into PDF
for page in "$tmpdir"/page-*.tiff
do
    base="${page%.tiff}"
    tesseract "$base.tiff" $base
done

# combine the pages into one txt
cat "$tmpdir"/page-*.txt > $output

rm -rf -- "$tmpdir"
2

Asprise OCR Library works on most versions of Linux. It can take PDF input and output as search PDF.

It's a commercial package. Download a free copy of Asprise OCR SDK for Linux here and run it this way:

aocr.sh input.pdf pdf

Note: the standalone 'pdf' specifies the output format.

Disclaimer: I am an employee of the company producing above product.

2
  • This post states that the product can do it, which is a helpful hint that should be posted as a comment. It doesn't explain how to actually solve the problem, which is what answers should do. Can you expand your answer so that someone can see how to do the solution?
    – fixer1234
    Mar 12, 2015 at 5:42
  • Thanks @fixer1234, I've edited it to include the command. Mar 12, 2015 at 10:17
2

As of June 2021 the best OCR solution which I found was gImageReader. I used the v.3.2.3 from Ubuntu 18.04 repository. It's using tesseract v.4.00.00aplha as a back-end.

It seems to be well maintained, has a nice GUI which is not bloated, and has all the features needed for relatively small tasks. I am using it for recognizing multi-page PDF scan files, sometimes of a very modest quality (<100 dpi, with artifacts). It does the job great. Seamlessly integrates with OpenOffice/LibreOffice dictionaries. All tesseract language and script files should be installed (this can be checked via Synaptic).

1

Try Apache PDFBox to extract text content from PDF File. In case of images embedded into PDF files use ABBYY FineReader Engine CLI for Linux to extract text.

3
  • I found ABBYY OCR to be pretty pitiful, one of the least capable programs I've tried. It might be adequate with a really clean image of standard font text of typical body text size, with no mixed fonts, mixed sizes, complex layout, graphics, lines, etc.
    – fixer1234
    Jan 3, 2015 at 8:56
  • Ya i also tried, it works fine. I have some doubt, can u help me? Jan 3, 2015 at 9:01
  • If what you need isn't covered in other answers here, the best thing to do is ask your own question. That will get it exposure to a lot of eyes.
    – fixer1234
    Jan 3, 2015 at 16:21
1

Simplest solution that actually worked for me:

pdftoppm in.pdf image
tesseract image-1.ppm text

This will output text.txt with the textual contents of a PDF. (I tried with a single-page image-content PDF.)

Note: Both of these commands don't like you to add/remove file extensions, for whatever reason; enter it in the exact fashion I showed above. Also, for some reason it outputs image-1.ppm instead of image.ppm.

5
  • 1
    You should, perhaps, have tried with a multipage PDF. It outputs image-N.ppm, where N is a number between 1 and the number of images in the PDF. You provide the extension on the input file, because neither neither program assumes the format of the input based on the extension and you could have called the input file in.pdfxxx (standard *nix behavior).
    – Auspex
    Sep 28, 2023 at 14:02
  • @Auspex While that's insightful, it still remains retarded that one would explicitly not be allowed to provide a format on the output but then be required to on the input, and the input not matching the output even still. It's just all-around a bad design.
    – Andrew
    Sep 30, 2023 at 4:45
  • It's really not. You aren't specifying a format on the input—you're specifying the entire path. The programs will check that the input is in an acceptable format. You can't provide a "format" on pdftoppm because it only outputs PPM files. It is entirely arguable that you should be able to specify the output file as image.exe if you want, because extensions are only suggestions on *nix systems. Similarly, tesseract (in its default mode—it's not obvious in either the man page or help how to output in PDF or hOCR) only outputs text, so let it choose the extension.
    – Auspex
    Sep 30, 2023 at 15:49
  • @Auspex Don't ever design something that needs to be user-friendly.
    – Andrew
    Oct 1, 2023 at 18:16
  • It's the Linux CLI. It's completely in line with other Linux commands. Is that user-friendly? Not really, but making something work in a way different from other Linux commands is definitely not user-friendly.
    – Auspex
    Oct 3, 2023 at 12:31

You must log in to answer this question.

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