35

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.

7
  • Duplicate? askubuntu.com/questions/16268/…
    – Jakob
    Jun 7, 2012 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, 2012 at 10:17
  • How, and what did you use to scan the document?
    – Mitch
    Jun 7, 2012 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, 2012 at 12:06
  • Scanning and/or OCR Software?
    – Mitch
    Jun 7, 2012 at 12:18

10 Answers 10

25

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
  • 7
    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, 2015 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, 2015 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. Mar 9, 2017 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, 2017 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! Jun 21, 2017 at 18:40
9

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

8

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

OCRmyPDF adds an OCR text layer to scanned PDF files, allowing them to be searched or copy-pasted.

ocrmypdf                      # it's a scriptable command line program
   -l eng+fra                 # it supports multiple languages
   --rotate-pages             # it can fix pages that are misrotated
   --deskew                   # it can deskew crooked PDFs!
   --title "My PDF"           # it can change output metadata
   --jobs 4                   # it uses multiple cores by default
   --output-type pdfa         # it produces PDF/A by default
   input_scanned.pdf          # takes PDF input (or images)
   output_searchable.pdf      # produces validated PDF output
2
  • 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, 2018 at 15:04
  • Took awhile but worked perfectly.
    – Planky
    Feb 26, 2020 at 19:56
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.

1
  • 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, 2015 at 18:31
2

I use ocrmypdf and it just works fine.

ocrmypdf input.pdf output.pdf --force-ocr

On a raspberry Pi I have created a sh file that converts all the files within that folder. Following content:

for i in *.pdf; do ocrmypdf "$i" "$i" --force-ocr;done

I call it by executing bash convertToSearchablePDF.sh in the terminal.

1

I needed to remove a bad OCR and reduce the size of my PDF as well; I came up with the following script using ocrmypdf and ghostscript.

#!/usr/bin/sh
TEMP_FILE="$(mktemp --suffix=.pdf)" &&
    ghostscript -q -dNOPAUSE -dBATCH -dSAFER -dPDFA=2 -dPDFACompatibilityPolicy=1 -dSimulateOverprint=true -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/ebook -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dFILTERTEXT -dImageResolution=300 -sOutputFile="$TEMP_FILE" "$1" &&
    ocrmypdf "$TEMP_FILE" "$2" &&
    rm "$TEMP_FILE"

The long ghostscript line removes the text layer and makes various space-saving changes to the first argument $1, saving them to a temporary file. We then add OCR with ocrmypdf (which is an excellent tool), and output to the path given by the second argument $2.

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"
0

You can use OCRthyPDF -> https://snapcraft.io/ocrthypdf

It is a frontend for ocrmypdf and available as SNAP.

You must log in to answer this question.

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