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

T

Ted Byers

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
 
T

Ted Byers

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
 
B

Bill H

Ted said:
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-
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top