G'day Kevin,

Firstly, I'm not a user of CAM::PDF; in fact, I didn't even have it installed. I suspect the getPageText() method is not the best choice for this: as you noted, you can't split lines easily and dollar amounts have an embedded space — I can't advise of a better choice; perhaps another monk can.

I would strongly recommend that you do not write lengthy regexes the way you did in the last example in your code: they are incredibly difficult to read; even more difficult to maintain; and extremely error-prone. See my code below for a much better way to do this. Also, take a look at Regexp::Debugger: I find it very helpful and, in fact, used it to check some of the fiddlier parts of the regex in the code below.

I've ignored the PDF download part of the code. You didn't ask about that: I'm assuming you've got that working satisfactorily. I just downloaded the two PDFs you referenced and accessed them from a local disk.

Some notes on how I've dealt with lack of information:

Here's the code:

#!/usr/bin/env perl use strict; use warnings; use constant { AMOUNT => 3, ADDL_RATE_PER => 4, DISCOUNT_PRICE => 6, }; use CAM::PDF; use Data::Dumper; my $jacket_id = $ARGV[0]; my $pdf_file = "pm_11113472_$jacket_id.pdf"; my $pdf = CAM::PDF::->new($pdf_file) or die $CAM::PDF::errstr; my $re = qr{(?x: \A \s*? ((?:A|)) # Awd \s+ (\d+-\d+) # Contractor Code \s+ ([^\$]+?) # Name \s+ (\$\s[0-9,.]+) # Amount \s+ (\$\s[0-9,.]+\s[A-Z]) # Add'l Rate/PER \s+ ([0-9.]+\s+\d+) # Discount % Days \s+ (\$\s[0-9,.]+) # Discount Price \s+ ([\D]+?) # Bidders Name \s+ (\S+) # Date Received \s+ (\(\d+\)\s\d+-\d+) # Phone Number )}; for my $page_num (1 .. $pdf->numPages) { my $text = $pdf->getPageText($page_num); my @lines; my $wanted_line = 0; for my $line (split /$jacket_id/, $text) { next unless $wanted_line++; my @fields = $line =~ $re; $fields[AMOUNT] =~ y/ //d; $fields[ADDL_RATE_PER] =~ s/ //; $fields[DISCOUNT_PRICE] =~ y/ //d; push @lines, [ $jacket_id, @fields ]; } print Dumper(\@lines); }

Here's the first part of the output using your first example PDF:

$ ./pm_11113472_pdf_parse.pl 746810 $VAR1 = [ [ '746810', 'A', '140-89226', 'UNION HOERMANN PRESS', '$844.00', '$15.00 C', '1 20', '$835.56', 'Randy Sigman', '01/22/2020', '(563) 582-3631' ], [ '746810', '', '190-38407', 'GRAPHIC VISIONS', '$869.00', '$140.00 M', '0.5 20', '$864.66', 'Howard Roskosky', '01/22/2020', '(301) 987-5586' ],

Open the spoiler to see full output for both example PDFs.

— Ken


In reply to Re: Regular Expression to Parse Data from a PDF by kcott
in thread Regular Expression to Parse Data from a PDF by kevyt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.