jorba has asked for the wisdom of the Perl Monks concerning the following question:

I cribbed some code from the internet to extract data from a pdf using CAM:PDF. It works fine. I then adapted it as part of a larger program. That doesn't work. So I'm doing something dumb. Appreciate it if someone could point out what it is.

Running on Windows.

Here's the cribbed code. It works fine, printing the data to the console.

use strict; use warnings; use CAM::PDF; use LWP::UserAgent; my $pdf_filename = 'C:\Users\Jay\Desktop\SBS DEV\test.pdf'; convert_pdf_to_text(); sub convert_pdf_to_text { use CAM::PDF::PageText; my $pdf_filename = 'C:\Users\Jay\Desktop\SBS DEV\test.pdf'; my $pdf = CAM::PDF->new($pdf_filename); my $y = $pdf->getPageContentTree(1); print CAM::PDF::PageText->render($y); }

Here's my code in full. The relevant bit is the line print CAM::PDF::PageText->render($content);

use strict; use warnings; use DBI; use CAM::PDF; my $db; my $sql; my $src; my $tgt; my $file; my $cnt; my @row; sub ConvertPDFToText { my $infn; my $fh; my $pdf; my $content; $infn = "$_[0]\\$_[2]"; open($fh, '>',"$_[1]" . "\\" . "Archive.txt"); print "filename $infn\n"; print "xx\n"; $pdf = CAM::PDF->new($infn); $content = $pdf->getPageContentTree(1); print CAM::PDF::PageText->render($content); return ""; } #Get db handle; $db = DBI->connect('DBI:mysql:SBS_Dev', 'DBProcess','ThhuSd73MIWAW +aY6') or die 'Cant Connect to DB'; # Get file directories $sql = $db->prepare('SELECT SRC_DIR, TGT_DIR FROM EXP_EXTRACT_CNTL + WHERE ID = 1') or die 'Couldnt run cntl sql: '. $db->errstr; $sql->execute(); @row = $sql->fetchrow_array(); ($src, $tgt) = @row; print "source $src\n"; print "target $tgt\n"; if ($sql->rows == 0) { die 'Control info not found'; } #Process Files from Source Directory opendir(DIR, $src) or die "Cant open Dir: $!"; while (($file = readdir(DIR))) { if ($file ne '.' and $file ne '..' and $file ne "Archive") { print "file $file\n"; #get data out of file ConvertPDFToText($src, $tgt, $file); $cnt = $cnt + 1; # Move file to archive rename "$src\\$file" => "$tgt\\$file"; } } closedir(DIR); print '$cnt files processed\n';

Here's the output from running the second one

C:\Users\Jay\Desktop\SBS DEV\CODE\perl>.\sbsextractfrompdf.pl source C:\Users\Jay\Desktop\SBS DEV\Data\Receipts target C:\Users\Jay\Desktop\SBS DEV\Data\Extracted file home depot large 2.pdf filename C:\Users\Jay\Desktop\SBS DEV\Data\Receipts\home depot large 2 +.pdf xx Can't locate object method "render" via package "CAM::PDF::PageText" a +t C:\Users \Jay\Desktop\SBS DEV\CODE\perl\SBSExtractFromPDF.pl line 31. C:\Users\Jay\Desktop\SBS DEV\CODE\perl>

As far as I can see, the relevant lines of code are identical as are the lines needed to get there. So what am I missing?

Thanx J.

Replies are listed 'Best First'.
Re: Cant locate object method CAM:PDF. I'm doing something dumb
by choroba (Cardinal) on Aug 14, 2017 at 12:32 UTC
    The line
    use CAM::PDF::PageText;

    isn't present in the "code in full". Is it just a copy&paste error?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Many thanx. That's fixed it. Apologies for the late response. For some reason I didnt get a notification of your answer