PDF::OCR::Thorough - extract text fom pdf document resorting to ocr as needed
use PDF::OCR::Thorough; my $abs_pdf = '/home/myself/file.pdf'; my $p = new PDF::OCR::Thorough($abs_pdf); my $text = $p->get_text;
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.
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.
takes no argument, checks if the pdf is ok, of PDF::API2 can open it This is called by constructor.
returns number of page files extracted
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 path to your original pdf provided as argument to constructor
returns filename of the original pdf provided as argument to constructor
returns abs path to where the temp copy of the pdf is
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.
argument is page number or abs path to page file (there is no page 0) returns text inside See also get_text()
returns all text in all pages, separated by \f newpage chars. See also get_page_text()
argument is abs path to image file returns ocr text this is also cached in object
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.
returns abs paths to burst pdf pages
will call cleanup() if DEBUG is not on and temp dir is in tmp
removes all temp content pretty rough, uses File::Path::rmtree() returns true.
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.
PDF::OCR PDF::API2
File::Copy, PDF::API2, PDF::GetImages, Image::OCR::Tesseract, File::Which
tesseract pdftk xpdf pdftotext
Leo Charre leocharre at cpan dot org
Copyright (c) 2007 Leo Charre. All rights reserved.
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".
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.
To install PDF::OCR, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PDF::OCR
CPAN shell
perl -MCPAN -e shell install PDF::OCR
For more information on module installation, please visit the detailed CPAN module installation guide.