28

I have a good quality scan of a document; such scan is in pdf format.

How can I add ocr information to the pdf, so that it becomes searchable? By searchable I mean that the goal is that when viewing the pdf with evince, CTRL-F actually allows me to search in the pdf content.

  • Duplicate? askubuntu.com/questions/16268/… – Jakob Jun 7 '12 at 9:04
  • 4
    @Jakob, I don't think it's a dupe, we are asking different things. The other question is about extracting text from some pdf (i.e. generating corresponding txt files), while my question is about modifying the pdf in order to add ocr information and make work the search function in the pdf reader. I'll clarify the question. – fdierre Jun 7 '12 at 10:17
  • How, and what did you use to scan the document? – Mitch Jun 7 '12 at 11:05
  • @Mitch I used my office Ricoh Aficio MP-C2500 printer/copier/scanner, which has a very nice document feeder. :-) – fdierre Jun 7 '12 at 12:06
  • Scanning and/or OCR Software? – Mitch Jun 7 '12 at 12:18
21

pdfsandwich

Does what you want and provides Ubuntu deb packages. It uses tesseract as OCR engine. The following call adds the text layer to your scanned PDF:

pdfsandwich scanned.pdf

Following does the same but with another language (ISO 639-2 code, download tesseract-ocr-LANGCODE package) and setting the layout:

pdfsandwich  -verbose -lang spa -layout single scanned.pdf

If you get any error please download last version deb from Sourceforge.

Disclaimer: I'm the developer of pdfsandwich and therefore obviously biased.

  • 6
    This is really great, thank you. However, it appears to modify the images, looks like it runs an unsharp mask over them or something. Is there a way to leave the images exactly as they previously were? In my particular instance, the filter even managed to remove the bar from a couple of fractions in some equations. Everything else works pretty well though... – naught101 Feb 9 '15 at 2:47
  • Bad quality package: `Lintian check results for /tmp/pdfsandwich_0.1.3_amd64.deb: E: pdfsandwich: control-file-has-bad-permissions md5sums 0664 != 0644 E: pdfsandwich: control-file-has-bad-owner md5sums james/james != root/root E: pdfsandwich: wrong-file-owner-uid-or-gid usr/ 1000/1000 E: pdfsandwich: wrong-file-owner-uid-or-gid usr/bin/ 1000/1000 E: pdfsandwich: wrong-file-owner-uid-or-gid usr/bin/pdfsandwich ... – A.B. Apr 22 '15 at 5:55
  • Download last version deb from SF. If you get an error at the end might be related to ghostscript (v0.1.4). Now v0.1.6 uses pdfunite. – Pablo Bianchi Mar 9 '17 at 21:46
  • 1
    @PabloBianchi Is there any way to manual proofreading of the OCRed text using pdfsandwitch? I'm doing this with some Swedish documents, and it works well, except for some misspellings (probably because of the original's font) which would be easy to fix if it was a text file, but how can I do this in the resulting PDF? – zrajm Jun 20 '17 at 15:44
  • @zrajm you can use some of pdfsandwich parameters for better recognition on OCR step. To edit hidden text behind image a PDF you can just edit text boxs layer with LibreOffice Draw, Inkscape or any PDF editing tool. If you find a better way please post it here. DaH jImej! – Pablo Bianchi Jun 21 '17 at 18:40
9

There are two projects which do the trick: GScan2PDF and OCRFeeder

4

I found a non-ideal solution, but a very effective one.

I use PDF X-Change Viewer through Wine. It has an OCR feature which adds a text layer to the existing image-based pdf.

Thus you can search and copy text from this invisible layer.

enter image description here

2

For a command line solution, you can use pdfocr.

In brief, install software:

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:gezakovacs/pdfocr
$ sudo apt-get update
$ sudo apt-get install pdfocr

Then run pdfocr:

$ pdfocr -i scanned.pdf -o scanned.with.search.pdf

That worked for me on Ubuntu 12.04 LTS.

  • 6
    Github here: github.com/gkovacs/pdfocr. But this has the same issue as pdfsandwich, as it modifies/compresses PDFs containing highres images, basically destroying some of the original image information. – jmiserez Mar 21 '15 at 18:31
2

A solution which is easily implementable and providing an output pdf with same quality of input file plus reasonable size is OCRmyPDF:

https://github.com/jbarlow83/OCRmyPDF

  • I have had excellent results with your script. Unlike pdfocr by Geza Kovacs, it does not require any extra (hard to compile in some Linux distros!) libraries. Thank you! – Maxim May 3 '18 at 15:04
0

This is my quick and dirty solution based on ImageMagick's convert, tesseract, parallel and pdftk (all available on debian-based distributions). It's largely based on this blog post.

#!/bin/sh -ex

density=${2:-"300"} # default to 300 DPI if 2nd parameter is not given

convert -monitor -density "$density" "$1" -monochrome -compress lzw -alpha deactivate page_%05d.tif
parallel --bar "tesseract {} {.} pdf 2>/dev/null" ::: page_*.tif
pdftk page_*.pdf cat output "${1%.*}-ocred.pdf" compress

# Cleanup temp files
rm page_?????.tif page_?????.pdf
0

For whole directory with ppm files you can use this script ppm2ocrpdf.sh

#!/bin/sh

mkdir .pdf
for f in *.ppm; do
    echo " Running convert -compress JPEG -quality 88 "$f" -page a4 "$f"ppm.pdf"
    convert -compress JPEG -quality 88 "$f" -page a4 "$f"ppm.pdf
    echo " Running tesseract -l deu "$f" "$f" pdf"
    tesseract -l deu "$f" "$f" pdf
    echo " Running pdftk "$f".pdf cat output ./.pdf/"$f"ocr.pdf"
    pdftk "$f".pdf cat output ./.pdf/"$f"ocr.pdf
    echo " Running rm "$f"ppm.pdf"
    rm "$f"ppm.pdf
    echo " Running rm "$f".pdf"
    rm "$f".pdf
done
echo " Running pdftk *.pdf cat output ../outdocument.pdf"
pdftk ./.pdf/*.pdf cat output outOcrDocument.pdf
echo " Running rm ./.pdf/*.pdf"
rm ./.pdf/*.pdf
echo " Running rmdir .pdf"
rmdir .pdf
echo "Done"

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.