Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Where is the documentation to show how to add a PNG file to a PDF

Reply
Thread Tools

Where is the documentation to show how to add a PNG file to a PDF

 
 
Ted Byers
Guest
Posts: n/a
 
      10-23-2008
I successfully created a number of PNG files using chart, and I
successfully created my PDF file. Both look quite nice.

I did find, in the documentation of PDF::API2 the image functions
(e.g. my $png1 = $pdf->image_png("chart1.png"), but these do not
actually add the image to the current page. I have looked through all
the documentation I have for PDFs, including PDF::API2, but I do not
see anything about how to actually add the png file (which I assume is
read by "image_png") to a specific page.

I did find the following for PDF::Report

"$pdf->addImg($file, $x, $y);
"Add image $file to the current page at position ($x, $y).

"$pdf->addImgScaled($file, $x, $y, $scale);
"Add image $file to the current page at position ($x, $y) scaled to
$scale. "

But I didn't use PDF::Report, yet, so I am concerned that shifting to
it will break the code I have already written. I use PDF::Table for
much of my PDF file. Do you know if it plays nicely with
PDF::Report? I see PDF::Report is described as a wrapper for
PDF::API2, so I would have thought that there would be something like
'addImg' in the PDF::API2 API, but I don't see it there.

I am looking fo rthe path of least resistance to get these images
added to specific pages in my PDF.

Any help would be appreciated.

Thanks

Ted
 
Reply With Quote
 
 
 
 
Ted Byers
Guest
Posts: n/a
 
      10-23-2008
On Oct 23, 12:32*am, Christian Winter <thepoet_nos...@arcor.de> wrote:
> Ted Byers wrote:
> > I successfully created a number of PNG files using chart, and I
> > successfully created my PDF file. *Both look quite nice.

>
> > I did find, in the documentation of PDF::API2 the image functions
> > (e.g. my $png1 = $pdf->image_png("chart1.png"), but these do not
> > actually add the image to the current page. *I have looked through all
> > the documentation I have for PDFs, including PDF::API2, but I do not
> > see anything about how to actually add the png file (which I assume is
> > read by "image_png") to a specific page.

>
> > I did find the following for PDF::Report

>
> > "$pdf->addImg($file, $x, $y);
> > "Add image $file to the current page at position ($x, $y).

>
> > "$pdf->addImgScaled($file, $x, $y, $scale);
> > "Add image $file to the current page at position ($x, $y) scaled to
> > $scale. "

>
> > But I didn't use PDF::Report, yet, so I am concerned that shifting to
> > it will break the code I have already written. *I use PDF::Table for
> > much of my PDF file. *Do you know if it plays nicely with
> > PDF::Report? *I see PDF::Report is described as a wrapper for
> > PDF::API2, so I would have thought that there would be something like
> > 'addImg' in the PDF::API2 API, but I don't see it there.

>
> > I am looking fo rthe path of least resistance to get these images
> > added to specific pages in my PDF.

>
> You'll have to look into the docs for PDF::API2::Content for the
> image methods (a lot of important information is strewn all over
> the different modules that come with PDF::API2). Basically, the image
> is added to the graphics canvas, which you have to retrieve first.
>
> my $img = $pdf->image_png("chart1.png");
> my $gfx = $page->gfx;
> $gfx->image( $img, $posx, $posy );
>
> HTH
> -Chris- Hide quoted text -
>
> - Show quoted text -


Thanks Chris. That is helpful. But now it appears that $gfx (I'd
guesed that $gfx, which I've seen a lot, was a canvas, but I have yet
to find the documentation of its interface) needs data that chart
doesn't appear to put into the png files it makes. For example, $gfx-
>image wants a height and width function/data member (which isn't

clear to me yet), the the image object returned by
'image_png("chart1.png");' doesn't have that. Providing a desired
width and height solves that, but then I find $gfx->image wants a name
from that object, and it isn't clear what that ought to be, what it is
used for, or how I can set it. There doesn't seem to be a way to pass
an arbitrary name as an argument. So I am lost as to what to do. I
don't know if the deficiency is in the png file my code produces using
Chart (these images are easily opened, without problems, by each of
the graphics packages I use for image editing), or if it is in how I
am using $gfx->image so far.

Thanks again

Ted
 
Reply With Quote
 
 
 
 
Bill H
Guest
Posts: n/a
 
      10-23-2008
On Oct 23, 2:36*pm, Christian Winter <thepoet_nos...@arcor.de> wrote:
> Ted Byers wrote:
> > Thanks Chris. *That is helpful. *But now it appears that $gfx (I'd
> > guesed that $gfx, which I've seen a lot, was a canvas, but I have yet
> > to find the documentation of its interface) needs data that chart
> > doesn't appear to put into the png files it makes. *For example, $gfx-
> >> image wants a height and width function/data member (which isn't

> > clear to me yet), the the image object returned by
> > 'image_png("chart1.png");' doesn't have that. Providing a desired
> > width and height solves that, but then I find $gfx->image wants a name
> > from that object, and it isn't clear what that ought to be, what it is
> > used for, or how I can set it. *There doesn't seem to be a way to pass
> > an arbitrary name *as an argument. *So I am lost as to what to do. *I
> > don't know if the deficiency is in the png file my code produces using
> > Chart (these images are easily opened, without problems, by each of
> > the graphics packages I use for image editing), or if it is in how I
> > am using $gfx->image so far.

>
> When the image method dies and calls for a name, it's usually due
> to the passed variable not being a valid image object as returned
> by the $pdf->image_* methods (probably a typo). PNGs created by
> the Chart modules do work with PDF::API2. I've put together a
> small test script that produces the desired output here:
> ################################################## #############
> #!/usr/bin/perl
>
> use Chart::Lines;
> use PDF::API2;
> use strict;
> use warnings;
>
> # Chart Creation
>
> my @data = ([1999,2000,2001,2002],[1234,2344,3544,2345]);
>
> my $chart = Chart::Lines->new( 500, 300 );
> # The tick formatter you've asked for in your other message:
> $chart->set( 'f_x_tick' => \&years );
>
> $chart->png( "test.png", \@data );
>
> sub years {
> * * * * return "Yr " . shift;
>
> }
>
> # PDF Creation
>
> my $pdf = PDF::API2->new();
> $pdf->mediabox("A4");
>
> my $fnt = $pdf->corefont('Helvetica-Bold');
>
> my $page = $pdf->page;
>
> my $gfx = $page->gfx;
>
> $gfx->textlabel( 200, 750, $fnt, 20, "Chart PNGs Work!" );
>
> my $img = $pdf->image_png( "test.png" );
> $gfx->image( $img, 50, 400, 500, 300 );
>
> $pdf->saveas( "test.pdf" );
> __END__
> ################################################## #############
>
> -Chris- Hide quoted text -
>
> - Show quoted text -


As Chris shows here, making a pdf is a step by step procedure that
builds on previous ones.

1st you make the "template":

my $pdf = PDF::API2->new();

Then you set the size of the pdf:

$pdf->mediabox("A4");

Then you add a page to the pdf:

my $page = $pdf->page;

Then you add a graphic "template" to the page:

my $gfx = $page->gfx;

Then you "load" the image:

my $img = $pdf->image_png( "test.png" );

And then apply this image to the graphic "template":

$gfx->image( $img, 50, 400, 500, 300 );

If you were making multiple, pages, you could now do another add page
and do things with it.

When you are finally done you save the pdf:

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

I work with PDF::API2 every day. It ia very powerful module if you
understand how it works.

Bill H
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Create thumbnail image (jpg/png) of PDF file using Python sophie_newbie Python 6 06-16-2012 10:43 AM
Postscript to PDF with pdf-tools, pdf-writer, or other Sean Nakasone Ruby 1 04-14-2008 09:13 PM
PDF::Writer, create pdf and insert in other pdf file. Ricardo Pog Ruby 1 03-26-2008 08:24 PM
ANN: PNG image CAPTCHA with PNG canvas available (SkimpyGimpy) aaronwmail-usenet@yahoo.com Python 0 04-18-2007 07:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57