The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

PDF::OCR::Thorough - extract text fom pdf document resorting to ocr as needed

SYNOPSIS

        use PDF::OCR::Thorough;

        my $abs_pdf = '/home/myself/file.pdf';

        my $p = new PDF::OCR::Thorough($abs_pdf);

        my $text = $p->get_text;

DESCRIPTION

unlike PDF::OCR which assumes each page in the pdf document is a page scan- This script is more thorough.

1) the original.pdf is copied to tmp.pdf 2) tmp.pdf is split into page1.pdf page2.pdf etc.. 3) for each pageX.pdf, first we try reading with pdftotext, if the result is too small we try to read with Image::OCR::Tesseract 4) the output of each is merged with newpage chars

The output to STDOUT is all the text of all pages, but it is separated with newpage characters. These can be matched with a regex \f

Please note the PDF::API2 is used to check that the pdf data is valid.

This is part of the PDF::OCR Package.

METHODS

new()

argument is the abs path to the pdf you want to read text from.

        my $p = new PDF::OCR::Thorough('/home/myself/myfile.pdf');

If the file is not there or the pdf data is corrupt, warns and returns undef.

pdf_data_ok()

takes no argument, checks if the pdf is ok, of PDF::API2 can open it This is called by constructor.

pages()

returns number of page files extracted

abs_tmp()

returns abs path to the temp dir created this is where the copy of your file resides, together with any images extracted, and page files extracted.

abs_pdf()

abs path to your original pdf provided as argument to constructor

filename()

returns filename of the original pdf provided as argument to constructor

abs_tmp_pdf()

returns abs path to where the temp copy of the pdf is

abs_images()

optional argument is abs path to a page file ( see abs_pages() ). if no argument provided, returns abs paths to all images extracted from all pages.

get_page_text()

argument is page number or abs path to page file (there is no page 0) returns text inside See also get_text()

get_text()

returns all text in all pages, separated by \f newpage chars. See also get_page_text()

get_ocr()

argument is abs path to image file returns ocr text this is also cached in object

force_ocr()

argument is boolean 1/0 force extracting images and running ocr even if pdftotext finds content returns value

You would want to set this to 1 if you expect your iamge to contain both text and large images perhaps with text also, and you want both extracted.

abs_pages()

returns abs paths to burst pdf pages

DESTROY

will call cleanup() if DEBUG is not on and temp dir is in tmp

cleanup()

removes all temp content pretty rough, uses File::Path::rmtree() returns true.

CAVEATS

Will not work with a corrupted pdf file. But it does test for that, so if it doesn't work, you know if it's because the PDF doc is messed up according to PDF::API2.

SEE ALSO

PDF::OCR PDF::API2

REQUIREMENTS

File::Copy, PDF::API2, PDF::GetImages, Image::OCR::Tesseract, File::Which

NON PERL REQUIREMENTS

tesseract pdftk xpdf pdftotext

AUTHOR

Leo Charre leocharre at cpan dot org

COPYRIGHT

Copyright (c) 2007 Leo Charre. All rights reserved.

LICENSE

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".

DISCLAIMER

This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the "GNU General Public License" for more details.