nargis has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I am trying to generate a pdf report with images using Perl. I am able to write text to the pdf file but I am getting error when trying to write images. Is anything wrong with my code? I am in Linux environment.
my $pdf = PDF::API2->new(); $pdf->mediabox(595,842); my $page = $pdf->page; my $fnt = $pdf->corefont('Arial',-encode => 'latin1'); my $txt = $page->text; $txt->textstart; $txt->font($fnt, 20); $txt->translate(150,800); $txt->text("Dige Images for Experiment"); my $photo = $page->gfx; my $jpeg = $pdf->image_jpeg("http://docs.gimp.org/images/filters/exam +ples/color-taj-sample-colorize.jpg"); $photo->image($jpeg,100,50,60,400); $txt->textend; $pdf->saveas("/tmp/HelloWorldDige24.pdf"); $pdf->end( );
The ERROR is:
error: Can't call method "val" on an undefined value at /usr/lib +/perl5/site_perl/5.8.0/PDF/API2/Resource/XObject/Image.pm line 99. context: ... 95: sub width { 96: my $self = shift @_; 97: my $x=shift @_; 98: $self->{Width}=PDFNum($x) if(defined $x); 99: return($self->{Width}->val); 100: } 101: 102: =item $ht = $img->height 103: ... code stack: /usr/lib/perl5/site_perl/5.8.0/PDF/API2/Resource/XObj +ect/Image.pm:99 /usr/lib/perl5/site_perl/5.8.0/PDF/API2/Resource/XObject/Image/JPEG.pm +:147 /usr/lib/perl5/site_perl/5.8.0/PDF/API2/Resource/XObject/Image/JPEG.pm +:81 /usr/lib/perl5/site_perl/5.8.0/PDF/API2/Resource/XObject/Image/JPEG.pm +:116 /usr/lib/perl5/site_perl/5.8.0/PDF/API2.pm:1898 /usr/local/apache/html/bioinfo/pfc/view/view_digegel.mhtml:86 /usr/local/apache/html/bioinfo/pfc/view/autohandler:107 /usr/local/apache/html/bioinfo/pfc/view/syshandler:42
  • Comment on Can't call method "val" on an undefined value at /usr/lib/perl5/site_perl/5.8.0/PDF/API2/Resource/XObject/Image.pm line 99
  • Select or Download Code

Replies are listed 'Best First'.
Re: Can't call method "val" on an undefined value at /usr/lib/perl5/site_perl/5.8.0/PDF/API2/Resource/XObject/Image.pm line 99
by Corion (Patriarch) on Apr 28, 2008 at 17:15 UTC

    From the documentation, it doesn't look as if ->image_jpeg accepts an URL. You could check that by checking the return value:

    my $imagename = "http://docs.gimp.org/images/filters/examples/color-ta +j-sample-colorize.jpg"; my $jpeg = $pdf->image_jpeg($imagename) or die "Couldn't add '$imagename' as a file.";
      Hi,
      I tried writing URl as well as direct image file, but none of them is working.