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

Hi:

I am trying to create a PDF document using API2. I need to include a png image that I create using GD::Graphs. When I try to read in the the image I get the following error:

substr outside of string at /usr/lib/perl5/site_perl/5.8.3/PDF/API2/PDF/ImagePNG.pm line 137.

I am running on RedHat 9. I am using Perl v5.8.3, GD v1.53, and API2 v0.3r77...these should all be the latest available revisions from CPAN. The png file I create can be read in through various Linux and Windoz utilities, so I am fairly sure the file is for the most part valid.

If anyone has experienced this problem, I would appreciate any advice you might have. Also, if anyone has any links to good tutorials on GD, GD::Graph, and/or API2, this will help me out a lot. It is not always easy to see how all the pieces fit together by looking at the pod.

TIA,

John

P.S. -- below is a stripped down version of what I am trying to accomplish...thanks!

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#!/usr/bin/perl -w use strict; use GD::Graph::bars; use PDF::API2; createGraph(); createPdf(); exit; sub createGraph { my @xLabels = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); + my @data2002 = qw( 17 19 26 38 56 64 67 53 40 29 21 13 ); my @data2003 = qw( 19 24 27 41 56 69 75 60 44 33 22 15 ); my @data = ( \@xLabels, \@data2002, \@data2003 ); my $graph = GD::Graph::bars->new( 800, 600 ); $graph->set( title => "Rainfall 2002/2003", y_label => "Millimetres" + ); my $image = $graph->plot( \@data ) or die( "Cannot create image" ); open OUT, ">demo.png"; binmode OUT; print OUT $image->png(); close OUT; } sub createPdf { my $hPdf = PDF::API2->new(-file => "demo.pdf"); my $hPage = $hPdf->page(); my $hGfx = $hPage->gfx(); $hPage->mediabox(300, 300); my $hImg = $hPdf->image('demo.png'); $hGfx->image($hImg, 0, 0); print $hPdf->stringify(); $hPdf->end; }
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

20040510 Edit by Corion: Changed formatting of code to use CODE tags
Edit by castaway, added readmore tags

Replies are listed 'Best First'.
Re: Problem using PNG images created by GD::Graphs in an API2 PDF doc
by dragonchild (Archbishop) on May 10, 2004 at 13:38 UTC
    Send the bug to the author. A quick glance of the code has a ton of bitshifting and bitmasking that I just don't want to figure out. *shudders*

    Ideally, I would write the testcase up as a .t file that the author can include in his/her tests from now on. (Plus, it's easier to debug that way.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      That is probably an excellent suggestion, but...I'm not sure where the problem actually resides. I spent the weekend trying to prove that it is either GD::Graphs or API2. I can create png files with GD::Graphs and view them in various utilites, and edit them with Gimp, which tells me GD::Graphs is working correctly. With API2 I can create PDFs and have them include png files that I have found laying about, which tells me that API2 is working correctly. Each seems to be working, I just can't get the two to play nicely together.

      John
Re: Problem using PNG images created by GD::Graphs in an API2 PDF doc
by Albannach (Monsignor) on May 10, 2004 at 14:22 UTC
    I don't pretend to know what is going on, but as I was bored, here are a couple of things that I did to your code, and I did end up with a functioning PDF:

    • use non-interlaced PNG with $image->interlaced(undef); in the PNG sub
    • throw in a $hPdf->save; at the end of the PDF sub
    • make sure your media box is large enough (at least 800x600 in your case)

    Hope this helps!

    Update: Versions I used (:

    • Perl 5.8.3 as ActiveState build 809 on Win2k
    • GD::Graph::bars 1.25
    • PDF::API2 0.3r77

    It also appears that the stringify is messing things up - either put the save before it or just remove it altogether as it serves no useful purpose (at least in this code fragment).

    --
    I'd like to be able to assign to an luser

      Thanks for taking the time to track down that info. For some odd reason this did not take care of the problem, I still get the same error. I am running the same package revisions as you are, with the only difference that I can see is that I am running Linux (RedHat 9) and you are running windows. Its hard (and scarey) to believe that this would make such a difference in the operation of these packages. Maybe I need to uninstall + re-install all packages to make sure that I have complete distributions. Thanks, John
      I tried your suggestions. What I did was:
          Added "$image->interlaced(undef);" in the line after I call the plot method.
          Added "$hPdf->save;" in the line after I called the stringify method.
          Changed the dimensions of the mediabox to be 1200x900.

      Unfortunately, after all this I am still having the same problem. When the PDF file is created it starts printing out "substr outside of string at /usr/lib/perl5/site_perl/5.8.3/PDF/API2/PDF/ImagePNG.pm line 137.".

      Do you know what versions of these packages you are running? Maybe there is some incompatability if I am on the bleeding edge of releases.

      Thanks for your help,
      John