Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Suppose I have a directory full of many PDFs. In most of them, the text is completely search-able, which is the way I need them to be. But a few of them are just image scans, and they need to be OCR-ed.

Other then simply doing a batch OCR on the entire directory, is there a way to quickly identify which PDFs are the image-only ones that actually need to be OCR-ed?

I'm not a programmer, but a linux-friendly solution would be preferred.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I'm not sure if this is a 100% solution, but I came up with the following script which should get you a good part of the way if not the whole way (I have not gone through the spec) It should be run from the directory which has all the PDF's (it will search subdirectories).

#! /bin/bash

if [[ ! "$#" = "1" ]]
  then
      echo "Usage: $0 /path/to/PDFDirectory"
      exit 1
fi

PDFDIRECTORY="$1"

while IFS= read -r -d $'\0' FILE; do
    PDFFONTS_OUT="$(pdffonts "$FILE" 2>/dev/null)"
    RET_PDFFONTS="$?"
    FONTS="$(( $(echo "$PDFFONTS_OUT" | wc -l) - 2 ))"
    if [[ ! "$RET_PDFFONTS" = "0" ]]
      then
          READ_ERROR=1
          echo "Error while reading $FILE. Skipping..."
          continue
    fi
    if [[ "$FONTS" = "0" ]]
      then
          echo "NOT SEARCHABLE: $FILE"
      else
          echo "SEARCHABLE: $FILE"
    fi
done < <(find "$PDFDIRECTORY" -type f -name '*.pdf' -print0)

echo "Done."
if [[ "$READ_ERROR" = "1" ]]
  then
      echo "There were some errors."
fi

It works by looking for the number of fonts specified in each PDF. If the file does not have any fonts it is assumed to be comprised only of an image. (This might trip up on password protected files, I have no idea, don't have any to test against). If there is some stuff which is searchable and some stuff which is an image, this won't work - but it will probably be useful to seperate scanned image documents in a PDF container from "real" PDF's.

You can, of-course, comment out the part of the if-then-else loop which does not apply if you only want to print out the files which are not searchable.

share|improve this answer
    
Looks promising! But when I run this script I think I am getting an error. It says: find: paths must precede expression: Anker2001.pdf Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression] "Anker2001.pdf" is the second file alphabetically in the directory. –  Brian Z Sep 7 '13 at 13:26
    
I've made a couple of changes to the script, which might fix your problem - you now run it and pass the directory you wish to check as an argument. I also fixed a big bug in the check by removing the "." after $FONTSFOUND !!! –  davidgo Sep 7 '13 at 19:15
    
This is great, thank you @davidgo! –  Brian Z Sep 8 '13 at 3:42
    
This seems strange, but it stopped working. Any idea why that would be? –  Brian Z Sep 10 '13 at 21:37
    
None whatsoever - I'd need to know more about how/when it stopped working, errors or what have you. I wonder if you have any PDF's with spaces in them, and if something something like that threw the script. (Not a solution, but a mitigation might be to throw in an additional line to move the checked files OUT OF THE PATH of the script and then rerun it, see where it gets stuck etc - and then move the files back). –  davidgo Sep 11 '13 at 3:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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