misterMatt has asked for the wisdom of the Perl Monks concerning the following question:
I'll post this in the code section once this is sorted out and it's working ok.#!usr/bin/perl -w use strict; use PDF::API2; #concatenates image files of type JPEG, GIF, and PNG in a specific dir +ectory into a pdf file that will #be spit out into that same directory upon completion #very fun print "Input a filepath to a directory (ending with a /. ex: 'C:/path/ +to/dir/'): "; my $dir = <STDIN>; chomp $dir; print "\nInput the name for the pdf file you wish to create (ex: 'pict +ures'): "; my $pdf_name = <STDIN>; chomp $pdf_name; #grab a list of files in our very special directory. my @images = sort glob("$dir*") or die "No images\n"; #xreate a new document with no pages my $pdf = PDF::API2->new; #loop through our images, create a page for each image # - while adding the image to the created page -> this is done in the +subs for my $file (@images){ #call sub based on what regex matches add_jpg($file) if ($file =~ /.jpg/); #add_png($file) if ($file =~ /.png/); #add_gif($file) if ($file =~ /.gif/); } $pdf->saveas("$dir$pdf_name.pdf"); #subs sub add_jpg{ my $jpg = shift; my $image = $pdf->image_jpeg($jpg); my $page = $pdf->page(); $page->mediabox(0,0,$image->width, $image->height); $page->trimbox(0,0,$image->width, $image->height); my $gfx = $page->gfx; $gfx->image($image, 0, 0); } sub add_png{ my $png = shift; my $image = $pdf->image_png($png); my $page = $pdf->page(); $page->mediabox(0,0,$image->width, $image->height); $page->trimbox(0,0,$image->width, $image->height); my $gfx = $page->gfx; $gfx->image($image, 0, 0); } sub add_gif{ my $gif = shift; my $image = $pdf->image_gif($gif); my $page = $pdf->page(); $page->mediabox(0,0,$image->width, $image->height); $page->trimbox(0,0,$image->width, $image->height); my $gfx = $page->gfx; $gfx->image($image, 0, 0); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: PDF::API2 error when adding images to pdf
by jethro (Monsignor) on Aug 20, 2009 at 15:33 UTC | |
by misterMatt (Novice) on Aug 20, 2009 at 18:11 UTC |