in reply to Filling out PDF forms with data from DBI?

merlyn,
Despite the advisory that it CAM::PDF might make formatting a problem, it is the only module I could figure out how to make work. Here is the most basic example:
#!/usr/bin/perl use strict; use warnings; use CAM::PDF; my $pdf = CAM::PDF->new('fw9.pdf') or die 'wtf'; # Use $pdf->getFormFieldList() to get list of field names # Unfortunately, fw9.pdf doesn't have descriptive names $pdf->fillFormFields('f1-1' => 'Randal Schwartz'); $pdf->cleanoutput('RSchwartz_w9.pdf');
As noted elsewhere in this thread, if all you are modifying is field values than the listpdffields.pl and fillpdffields.pl utilities that come with CAM::PDF may come in handy.

Note: I did have some troubles installing the module and its dependencies but assume it was due to local environment. Let me know if you have problems so I can share workarounds.

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Filling out PDF forms with data from DBI?
by mercutio_viz (Scribe) on Jun 24, 2006 at 07:06 UTC

    It looks like L~R is right on the money with CAM::PDF. I've spent a lot of time with PDF::API2 but I think that CAM::PDF is better suited for this task. In the case of of the aforementioned W-9 form, with its non-descript field names, I wrote an inelegant-but-useful piece of code that helps to identify the actual field locations:

    #!/usr/bin/perl # pdf-filler-test.pl use strict; use warnings; use CAM::PDF; # my $infile = 'fw9.pdf'; my $outfile = 'modified_fw9.pdf'; my $pdf = CAM::PDF->new($infile) or die 'wtf'; my @FIELDS = $pdf->getFormFieldList(); # foreach ( @FIELDS ) { my $fieldnum = $_; $fieldnum =~ s/f1-//; $pdf->fillFormFields($_ => $fieldnum); } $pdf->cleanoutput($outfile);

    This revealed that each digit in the SSN and tax ID number fields is actually a form field. I would assume that Randal would need to analyze each form so as to know which fields are what, and then create the appropriate mapping from DBI fields to PDF form fields.

    On this particular form I noticed that the check boxes were not form fields. Randal, does your client require a check mark on the document, e.g. for "sole proprietor" or "Corporation?" The only reason I ask is because I don't see an easy means to add a check mark using CAM::PDF. PDF::API2 or PDF::Reuse can do that easily. However, they don't have the easy interface into forms the way CAM::PDF does. It might be a bit of a kludge, but running through CAM::PDF to fill in the forms and then running through PDF::API2 to add any checkmarks (or any kind of glyphs or graphics) would be pretty easy. To add a check mark on "Corporation" you could just do this:

    #!/usr/bin/perl use strict; use warnings; use PDF::API2; # location of "Corporation" check box is: # 173 pts from left, 660 pts from bottom my $infile = 'modified_fw9.pdf'; my $outfile = 'modified_check_corp_box_fw9.pdf'; my $pdf = PDF::API2->open($infile); my $page = $pdf->openpage('1'); my $text = $page->text(); my $font = $pdf->corefont('Helvetica'); # prepare text object $text->font($font,11); # Set font to Helvetica, 11pt $text->fillcolor("#000000"); # This is black $text->translate(173,660); # Text start location for Corp chk box $text->text("X"); # Print "X" at 173,660 $pdf->saveas($outfile); $pdf->end;

    These are just some thoughts. Please let us know what you eventually come up with. I'm curious to see how this looks in a production environment.

    -MC