Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

PDF::API2::Lite adding images

by misterMatt (Novice)
on Aug 17, 2009 at 23:27 UTC ( [id://789305]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys. I wrote a tiny script that creates a pdf file by searching a directory for a list of jpg image files and then adding them to separate pages inside the pdf. It works fine, thanks to the example code I found online (the PDF::API2::Lite docs really really suck), but I was wondering if I could make it work more generic by having the ability to add any images in a directory, like gifs, pngs, ect. - not just jpg files. I was under the impression that $pdf->image($file) would be able to do that. I have tried using my $image = $pdf->image($file);, and then $pdf->image($image, 0, 0);to then add the image to a page that I created but I get this error: 'Can't call method "image" on an undefined value'. Here is the code I have that works:
#!usr/bin/perl -w use strict; use PDF::API2::Lite; #concatenates all files in a specific directory into a pdf file that w +ill #be spit out into that same directory upon completion #grab a list of files in our very special directory. my @files = sort glob('path\to\jpgs\*') or die "No files"; #xreate a new document with no pages my $pdf = PDF::API2::Lite->new; #loop through our images, create a page for each image # - while adding the image to the created page for my $file (@files){ my $image = $pdf->image_jpeg($file); $pdf->page($image->width, $image->height); $pdf->image($image, 0, 0); } $pdf->saveas('output.pdf');

Replies are listed 'Best First'.
Re: PDF::API2::Lite adding images
by Anonymous Monk on Aug 19, 2009 at 00:12 UTC
    Instead of using "image_jpeg($file)" you have to use the various specialized image functions, such as "image_gif($file)" and "image_png($file)". See a fuller list of image type commands here:

    http://search.cpan.org/~areibens/PDF-API2-0.73/lib/PDF/API2/Lite.pm

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://789305]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found