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

I am trying to use PDF::API2 to create variable "postcards". Ultimately I need to create a card with variable png images (with transparencies so it all needs to be 24 bit) over a jpg background. This will be driven by a csv file for data creating 1 card per record. I already have the data upload portion done, I'm just trying to write a pdf file.
#!/usr/bin/perl # process_pdf.cgi use PDF::API2; my $pdf = PDF::API2->new(-file => "font_test.pdf"); $pdf->mediabox(647,435); $pdf->trimbox(9,9,629,417); my $page = $pdf->page; my $fnt = $pdf->corefont('Arial',-encode => 'latin1'); my $jpeg = $pdf->image_jpeg('../html/font_test/Beach/background.jpg'); @imgs=( $pdf->image_png('../html/font_test/Beach/B.png'), $pdf->image_png('../html/font_test/Beach/I.png'), $pdf->image_png('../html/font_test/Beach/L.png')); my $txt = $page->text; $txt->textstart; $txt->font($fnt, 12); $txt->translate(200,100); $txt->text("Bill OConnell"); $txt->translate(200,120); $txt->text("Stay Late!"); $txt->translate(200,140); $txt->text("Work hard!"); $txt->textend; foreach my $img (@imgs) { $page=$pdf->page; $page->mediabox($img->width,$img->height); $gfx=$page->gfx; $gfx->image($img,0,0,1); } $page->mediabox($jpeg->width,$jpeg->height); $gfx=$page->gfx; $gfx->image($jpeg,0,0,1); $pdf->save; $pdf->end( );
This currently creates a 4 page pdf document (the L is missing?) and the imgs array png's are excessively large. I am having a very difficult time finding in depth documentation on this module. Any suggestions or pointers in regards to module usage would be appreciated - and I'm in no way opposed to using a different approach, although HTML::HTMLDoc would not install on my system -seems like it has some compatibility issues with my kernel 2.6.24 since this was listed in all of the "failed" installs Thanks, Bill

Replies are listed 'Best First'.
Re: PDF::API2 question in images
by jds17 (Pilgrim) on Jul 01, 2008 at 20:55 UTC
    The image L.png is missing because you don't start a new page before adding the jpeg, so the jpeg ends up covering the png.

    You can control the image size e.g. by using an art box. The positioning can be taken care of using the image method (taking as parameters the image, the left x coordinate, the lower y coordinate and a scaling factor). To get the desired result in your program, please check replacing the lines starting with "foreach my $img (@imgs) {..." by the following code, using example coordinates $min_x, $min_y for better illustration:

    my $min_x = 150; my $min_y = 100; foreach my $img (@imgs) { $page=$pdf->page; $page->artbox(0, 0, $img->width,$img->height); $gfx=$page->gfx; $gfx->image($img, $min_x, $min_x, 1); } $page=$pdf->page; $page->artbox($jpeg->width,$jpeg->height); $gfx=$page->gfx; $gfx->image($jpeg, $min_x, $min_y, 1); $pdf->save; $pdf->end();
    I feel the module's documentation on CPAN is quite good, besides there are wrapper modules if you want a simpler interface (e.g. PDF::API2::Simple). Also, PDF is a well documented standard, so you can use its documentation, and since PDF::API2 does not rename entities, you should be able to look up things in case the module's documentation is not sufficient.