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');