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');
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.