| 1 | package PDF::OCR2::Page; | ||
| 2 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use vars qw($VERSION $DEBUG @TRASH $CHECK_PDF $NO_TRASH_CLEANUP); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use LEOCHARRE::Class2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | #__PACKAGE__->make_constructor_init; | ||
| 6 | __PACKAGE__->make_accessor_setget('errstr'); | ||
| 7 | __PACKAGE__->make_count_for('abs_images'); | ||
| 8 | __PACKAGE__->make_accessor_setget_ondisk_file( 'abs_pdf' ); | ||
| 9 | 3 | ▶ | use PDF::OCR2::Base; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Carp; | ||
| 11 | use warnings; | ||
| 12 | $VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)/g; | ||
| 13 | |||
| 14 | |||
| 15 | sub new { | ||
| 16 | my($class,$arg) = @_; | ||
| 17 | |||
| 18 | my $self = | ||
| 19 | ('HASH' eq (ref $arg)) ? $arg : | ||
| 20 | $arg ? { abs_pdf => $arg } : {}; | ||
| 21 | |||
| 22 | bless $self, $class; | ||
| 23 | |||
| 24 | if (my $arg = $self->{abs_pdf}){ | ||
| 25 | no warnings; | ||
| 26 | $CHECK_PDF and $PDF::OCR2::CHECK_PDF = 1; # hack | ||
| 27 | # this checks the pdf with PDF::API2 if PDF::OCR2::CHECK_PDF or CHECK_PDF are set | ||
| 28 | ( $self->{abs_pdf} = PDF::OCR2::Base::get_abs_pdf($arg) ) or return; | ||
| 29 | } | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub abs_images { | ||
| 35 | my $self = shift; | ||
| 36 | unless( $self->{abs_images} ){ | ||
| 37 | my $abs = $self->abs_pdf; | ||
| 38 | |||
| 39 | debug("calling PDF::GetImages for '$abs'.. "); | ||
| 40 | require PDF::GetImages; | ||
| 41 | my $images = PDF::GetImages::pdfimages($abs); | ||
| 42 | |||
| 43 | defined $images or $self->errstr("PDF::GetImages::pdfimages($abs) does not return."); | ||
| 44 | scalar @$images or $self->errstr("PDF::GetImages::pdfimages($abs) does not return values.. no images?"); | ||
| 45 | $self->{abs_images} = $images; | ||
| 46 | $self->{abs_images} ||= []; | ||
| 47 | debug("DEBUG is ON"); | ||
| 48 | } | ||
| 49 | wantarray ? @{$self->{abs_images}} : $self->{abs_images}; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub _text_from_pdf { | ||
| 54 | #$_[0]->{_text_from_pdf} ||= $_[0]->_system_pdftotext( $_[0]->abs_pdf ); | ||
| 55 | $_[0]->{_text_from_pdf} ||= $_[0]->_cam_pdftotext( $_[0]->abs_pdf ); | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub _text_from_images { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | my $txt; | ||
| 63 | for my $abs ( @{$self->abs_images} ){ | ||
| 64 | $txt.= $self->_text_from_image($abs); | ||
| 65 | } | ||
| 66 | $txt; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _length_from_images { length $_[0]->_text_from_images || 0 } # will attempt creation of images | ||
| 70 | sub _length_from_pdf { length $_[0]->_text_from_pdf || 0 } # will call pdftotext | ||
| 71 | sub _length { length $_[0]->_text || 0 } # will call all | ||
| 72 | |||
| 73 | |||
| 74 | sub _text_from_image { | ||
| 75 | my($self,$abs) = @_; | ||
| 76 | unless( $self->{_text_from_image}->{$abs} ){ | ||
| 77 | |||
| 78 | # -f $abs ? print STDERR" ===== $abs is on disk \n" : confess("No $abs on disk"); | ||
| 79 | |||
| 80 | require Image::OCR::Tesseract; | ||
| 81 | my $txt = Image::OCR::Tesseract::get_ocr($abs); | ||
| 82 | $self->{_text_from_image}->{$abs} = $txt; | ||
| 83 | push @TRASH, $abs; | ||
| 84 | } | ||
| 85 | $self->{_text_from_image}->{$abs}; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub text { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | unless($self->{text}){ | ||
| 92 | |||
| 93 | # text from pdf first | ||
| 94 | my $txt = $self->_text_from_pdf; | ||
| 95 | # check the length now ? | ||
| 96 | |||
| 97 | debug( "LENGTH A, from pdf regular text ".length($txt)); | ||
| 98 | |||
| 99 | # now text from images | ||
| 100 | $txt.= $self->_text_from_images; | ||
| 101 | $self->{text} = $txt; | ||
| 102 | |||
| 103 | debug( "LENGTH B, from images/ocr ".length($txt)); | ||
| 104 | |||
| 105 | } | ||
| 106 | $self->{text}; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | # CAN BE REUSED ................. : | ||
| 118 | |||
| 119 | sub debug { $DEBUG ? print STDERR __PACKAGE__." DEBUG @_\n" : 1 } | ||
| 120 | |||
| 121 | |||
| 122 | sub _cam_pdftotext { | ||
| 123 | my($self,$abs) = @_; | ||
| 124 | require CAM::PDF; | ||
| 125 | my $o = CAM::PDF->new($abs); | ||
| 126 | my $text = $o->getPageText(1); # the whole point is this package does 1 ONE page. | ||
| 127 | return $text; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | # so it can be re used: | ||
| 132 | sub _system_pdftotext { | ||
| 133 | my ($self,$abs) = @_; | ||
| 134 | |||
| 135 | my $bin = `which pdftotext`; | ||
| 136 | chomp $bin; | ||
| 137 | |||
| 138 | |||
| 139 | my @cmd = ( $bin, '-q', $abs ); | ||
| 140 | |||
| 141 | debug(@cmd); | ||
| 142 | |||
| 143 | my $absout = $abs; | ||
| 144 | $absout=~s/\.pdf/.txt/i; | ||
| 145 | |||
| 146 | local $/; | ||
| 147 | open(OUT,'<',$absout) or $self->errstr("could not open '$absout' for reading, $!") and return; | ||
| 148 | my $t = <OUT>; | ||
| 149 | close OUT; | ||
| 150 | push @TRASH, $absout; | ||
| 151 | $t; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub DESTROY { unlink @TRASH unless ( $DEBUG or $NO_TRASH_CLEANUP ) } | ||
| 155 | |||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | |||