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

Hi, I've got a rather unusual problem trying to: 1. Generate GIF image file. 2. Include this GIF file into PDF. If the GIF file size is more than a certain number of bytes - the image looks cut off in PDF (it looks OK if viewed separately). No such problem exists for PNG files, but unfortunately I have to use GIFs. Here is graph generation code:
use GD; use GD::Graph::Data; use GD::Graph::bars; my $data = GD::Graph::Data->new( [["2001", "2002", "2003", "2004", "2005", "2006", "2007", "2 +008", "2009", "2010"], [ 10.2, 7.5, 4.8, 5.5, 5.5, 5.3, 4.6, 5.3, 5.7, 3.6], [ 9.8, 8.4, 4.4, 5.2, 5.3, 5.2, 4.9, 6.1, 6.3, 3.8]]); my $graph = new GD::Graph::bars( 1200, 800 ); $graph->set( title => 'Test Graph', types => [qw(bars bars)], transparent => 0, bar_spacing => 0, bargroup_spacing => 20, show_values => 1, x_labels_vertical => 1, interlaced => 0, ); my $gd = $graph->plot( $data ); open (MYFILE, '>test.gif'); binmode MYFILE; print MYFILE $gd->gif(); close (MYFILE);
following by the PDF generation:
use strict; use warnings; use PDF::API2; my $pdf = PDF::API2->new( -file => "sample.pdf" ); my $page = $pdf->page; $page->mediabox(792,612); my $photo = $page->gfx; my $photo_file = $pdf->image_gif('test.gif'); $photo->image( $photo_file, 100,100,600,400); $pdf->save; $pdf->end();
Could you please help me to figure it out? By the way, the problem is consistent whether I use Perl under Windows or under Solaris. Thanks in advance.

Replies are listed 'Best First'.
Re: Including GIF image into PDF (using GD::Graph and PDF::API2 libraries)
by zentara (Cardinal) on Feb 02, 2009 at 19:53 UTC
    Yeah, I see your problem too, and I see the fix ( or at least the direction). You are making a huge gif, then trying to squeeze it into a smaller media box. I'm not an expert on pdf, but the following changes prevent the cutoff.
    my $graph = new GD::Graph::bars( 600, 400 ); ...... $page->mediabox(800,600); ......
    It seems the media box needs to be bigger than the gif, and possibly there are centering and page size considerations with pdf. Maybe someone else knows more, like a command to set the page size in the pdf.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Thanks, but this only make to avoid the problem with the data I used in this example. If I make the GIF area smaller (600x400), but use diffent numbers, I still have this problem:
      ... my $data = GD::Graph::Data->new( [["2001", "2002", "2003", "2004", "2005", "2006", "2007", "2 +008", "2009", "2010"], [ 10.2, 7.5, 4.8, 5.5, 5.5, 5.3, 4.6, 5.3, 5.7, 3.6], [ 9.8, 9.9, 9.8, 9.9, 9.8, 9.9, 9.8, 9.9, 9.8, 9.9]]); ... my $graph = new GD::Graph::bars( 600, 400 ); ...
        I don't understand your artificial restriction to gif? If it works as png, why not save the gif, convert to png, then put the png into your pdf?

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Including GIF image into PDF (using GD::Graph and PDF::API2 libraries)
by spx2 (Deacon) on Feb 02, 2009 at 23:53 UTC
    I believe you should drop PDF::API2 and resort to LaTeX where you can manipulate
    images much easier.
    Basically you can use some tags in LaTeX to do it like this. That's really just one way to do it,in LaTeX there are many packages that allow you to include
    images in just about all kinds of formats and with lots of options which refer to positioning etc.
Re: Including GIF image into PDF (using GD::Graph and PDF::API2 libraries)
by Anonymous Monk on Mar 09, 2009 at 21:13 UTC
    I have to wonder: Why even create the GIF file at all?

    ####Remove this snippet:

    open (MYFILE, '>test.gif');
    binmode MYFILE;
    print MYFILE $gd->gif();
    close (MYFILE);

    #### and change this line:

    my $photo_file = $pdf->image_gif('test.gif');

    #### to this:

    my $photo_file = $pdf->image_gd($gd);

    That way you don't have a gif file floating around, it is created directly into the PDF.
      Going back to the original post, there does appear to be a bug in the GIF reader in PDF::API2, which causes it to decompress larger size GIF files incorrectly. I managed to trace through and fix it for my project. The relevant library file GIF.pm, version 2.0, 2005/11/16 contains an error in line 135. The patch is as follows:
      C<< 135c135 < $bits++ if($nextcode == (1<<$bits)); --- > $bits++ if($nextcode == (1<<$bits) and $bits<12); >>
      I will submit this bug/fix to the package maintainer but in the meantime this post should help people who are stuck get their program working again.

        When generating submitting your patch, can you generate a .t file as well that exhibits the incorrect behavior in the original, but not the patched version.

        Not certain how you would do this, but there may be an example in the source .t files.

        --MidLifeXis

        thanks a lot. works for me also.