Perl programmer for hire: download my resume (PDF).
John Bokma MexIT
freelance Perl programmer

JPEG images to PDF using Perl

Tuesday, February 24, 2009 | 2 comments

Earlier this month I downloaded several episodes of the comic "Suske en Wiske" (Spike and Suzy), as cbr files. I own the original dead tree copies, but they are still in the Netherlands, and I own too many to ask my mom to take them with her when she visits us. Besides, we're running out of storage space in our little apartment.

A cbr file is a collection of image files, scans of the actual comic book pages, archived using the RAR format, hence the last letter. And cb stands for comic book. I already read two "Suske en Wiske" comic books during our short vacation in Puerto Escondido earlier this month using "XnView Pocket" on my PDA, a Dell Axim X51v, by opening each image file (JPEG). But reading using XnView was not an easy task.

Just before we left I considered creating a PDF file with one page (JPEG) per page, and use PocketXpdf to read it. But there was no time left to code a small Perl solution to achieve this.

And tonight I had some spare time, and decided to give it a try. Below follows a simple Perl program that reads all JPEG images from a directory named "test" and creates a PDF file "test.pdf" with one image per page.

#!/usr/bin/perl

use strict;
use warnings;

use PDF::API2::Lite;

my $dir = 'test';

opendir my $dh, $dir
    or die "Can't opendir '$dir': $!";
my @jpg = grep -f "$dir/$_" && /\.jpe?g/i, readdir $dh;
closedir $dh
    or die "Can't closedir '$dir': $!";

my $pdf = PDF::API2::Lite->new;

for my $jpg ( sort @jpg ) {

    my $image = $pdf->image_jpeg( "$dir/$jpg" );
    $pdf->page( $image->width, $image->height );
    $pdf->image( $image, 0, 0 );
}

$pdf->saveas( 'test.pdf' );

The dimension in pixels of each page is taken from the dimension of each JPEG image. Next, the image object is added to the left top of each page. The image files are read and added sorted in standard string comparison order, the default of the sort function.

The above Perl code is a simple example. Extending this example and adding additional error handling is left as an excercise to the reader. I might soon post a Perl program that has more functionality, but this example is already a good start to code your own solution.

As for now being able to read comic books on my Dell Axim using PocketXpdf, no such luck yet. Maybe PocketXpdf needs too much memory in order to display the comic. I will test later with a smaller pdf file. If that works, I just create for each comic book several pdf files, each file containing several pages of the book.

Perl and PDF related

Also today

Please post a comment | read 2 comments, latest by Ray | RSS feed