I created a key in Windows registry to have an option on every folder which allows me to simply right click, choose the right option and have a PDF made, either from JPGS, or PNGs, or TIFs.
My registry key is as follows:
'"C:\strawberry\perl\bin\perl.exe" "C:\Users\michal\Documents\createpdf\790024.pl" "%1"'.
The code, thanks to suggestions and some major modifications by Matt Jacob works. Dealing with paths with spaces is done by:
my $folder = $ARGV[0];
$folder =~ s|\\|/|g;
$folder =~ s|/$||;
To read files I have came up with this: my @files = <"$folder/*">;
As I am a Perl newbie, I would like to see if this is correct. It works on my system.
The whole script is as follows:
#!/usr/bin/perl
use PDF::API2;
use strict;
use warnings;
my $folder = $ARGV[0];
$folder =~ s|\\|/|g;
$folder =~ s|/$||;
my $pdf_file = $folder . '.pdf';
die "Not a folder!\n" unless -d $folder;
die "There's already a pdf of that name!\n" if -f $pdf_file;
my $pdf = PDF::API2->new;
my %dispatch = (
jpg => sub { $pdf->image_jpeg(@_) },
png => sub { $pdf->image_png(@_) },
tif => sub { $pdf->image_tiff(@_) },
);
my @files = <"$folder/*">;
for my $ext (sort(keys(%dispatch))) {
my $count;
for my $file (@files) {
next unless $file =~ /\.$ext$/;
my $image = $dispatch{$ext}->($file);
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);
$count++;
}
last if $count;
}
if ($pdf->pages) {
$pdf->saveas($pdf_file);
}