Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

PDF::API2 error when adding images to pdf

by misterMatt (Novice)
on Aug 20, 2009 at 08:17 UTC ( #790024=perlquestion: print w/replies, xml ) Need Help??
misterMatt has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys. I gained a lot of ground since i asked my last question on this subject - and I have the script working perfectly for jpg images. The jpg files are added to the pdf without incident, but when I uncomment the lines that add png or gif files to the pdf document the script fails and gives me two different errors: one for png images, and one for gif images. They are: (1)Can't call method "val" on an undefined value at C:/Perl/site/lib/PDF/API2/Resource/XObject/ line 99. and (2)unsupported graphic control extension (255) at C:/Perl/site/lib/PDF/API2/Resource/XObject/Image/ line 229. I'm not exactly sure that the errors are tied to a specific format - but they seem to correlate the way I posted when I comment out the png line, or I comment out the gif line. Anyway. I'm absolutely not at all sure what is causing these errors - because the code is exactly identical to the jpeg image code, and that works without any hiccups. Any ideas guys? Here is the code I'm looking at right now with the two lines that are causing problems commented out inside of the for loop:
#!usr/bin/perl -w use strict; use PDF::API2; #concatenates image files of type JPEG, GIF, and PNG in a specific dir +ectory into a pdf file that will #be spit out into that same directory upon completion #very fun print "Input a filepath to a directory (ending with a /. ex: 'C:/path/ +to/dir/'): "; my $dir = <STDIN>; chomp $dir; print "\nInput the name for the pdf file you wish to create (ex: 'pict +ures'): "; my $pdf_name = <STDIN>; chomp $pdf_name; #grab a list of files in our very special directory. my @images = sort glob("$dir*") or die "No images\n"; #xreate a new document with no pages my $pdf = PDF::API2->new; #loop through our images, create a page for each image # - while adding the image to the created page -> this is done in the +subs for my $file (@images){ #call sub based on what regex matches add_jpg($file) if ($file =~ /.jpg/); #add_png($file) if ($file =~ /.png/); #add_gif($file) if ($file =~ /.gif/); } $pdf->saveas("$dir$pdf_name.pdf"); #subs sub add_jpg{ my $jpg = shift; my $image = $pdf->image_jpeg($jpg); 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); } sub add_png{ my $png = shift; my $image = $pdf->image_png($png); 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); } sub add_gif{ my $gif = shift; my $image = $pdf->image_gif($gif); 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); }
I'll post this in the code section once this is sorted out and it's working ok.

Replies are listed 'Best First'.
Re: PDF::API2 error when adding images to pdf
by jethro (Monsignor) on Aug 20, 2009 at 15:33 UTC

    There is a bug with your regexes. If you want to find a gif filename, you have to use something like this:

    add_gif($file) if ($file =~ /\.gif$/);

    Otherwise you will also find a file bgifblue.txt for example. The same problem is with the png and jpeg regexes

    If this doesn't solve the issues, read on:

    How many of your gifs (or pngs) cause the error? All of them or only one? If you look at the code in, it dies when it finds anything else but a 0xF9 after identifying a Graphics Control Extension, which seems to be used for specifiying the animation in animated gifs.

    I can think of a few possibilities:

    1) If you only have one gif with the problem, it likely is a corrupted gif.

    2) If you have only one or a few gifs with the problem, it might be some never version of gif, where there could be something other than 0xF9 at that position

    3) If all your gifs have this problem then the parser in is probably bugged

    I checked the CPAN source of PDF::API2::Resource::XObject::Image for the png error and I don't see how an undefined value can surface there. Either I didn't see the bug or maybe you have an older version of any of the libraries PDF::API2::Resource::XObject::Image, PDF::API2::Basic::PDF::Utils, PDF::API2::Basic::PDF::Number or PDF::API2::Basic::PDF::String

      Wow you were absolutely right! I tightened up the regex patterns like you suggested, and I found and removed an animated gif. It works just fine now - a little slower than just jpeg files, but it works great. Thank you for your wonderful suggestions!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://790024]
Approved by rovf
Discipulus ignores..
Discipulus IE just maps to id est in my mind .. ;=)
[Corion]: marto: I believe nowadays, at least window.opener should not be set anymore (except maybe within the same domain)
[Corion]: But I wouldn't really know as I don't use iexplore much (except at $work) and mostly surf with JS disabled (except at $work :) )

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2018-03-21 11:45 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (267 votes). Check out past polls.