Is it possible to add header(with text and one image) and footer (with page number) with images. I wrote below code to create a PDF document which shows png images.
If this can be done easily with any other module, please suggest.Really appreciate response with sample code.
use strict;
use PDF::API2::Lite;
use Getopt::Long;
my $outfile;
my $path;
my $options = GetOptions( "outfile=s" => \$outfile,
"images=s" => \$path,);
my @images = sort glob("$path") or die "No Files\n";
my $pdf = PDF::API2::Lite->new();
for my $png ( sort @images ) {
my $image = $pdf->image_png( "$png" );
$pdf->page(1150,450);
$pdf->image($image, 10, 10);
}
$pdf->saveas( $outfile );
Waiting one day on SO saved you 10 minutes reading the module documentation. It's not difficult, Space.
use PDF::API2 qw();
{
my $pdf = PDF::API2->open('input.pdf');
for my $index (1 .. $pdf->pages) {
my $page = $pdf->openpage($index);
my $txt = $page->text;
$txt->textlabel(300, 700, $pdf->corefont('Helvetica Bold'), 12, 'some Header text');
my $gfx = $page->gfx;
$gfx->image($pdf->image_png('Header_image.png'), 150, 700);
$txt->textlabel(300, 100, $pdf->corefont('Helvetica Bold'), 12, "Page: $index");
}
$pdf->saveas('output.pdf');
$pdf->end;
}
PDF::API2 is my workhorse for this sort of thing.
And I'll almost always use the importPageIntoForm
method, as soon as I need to do any layup or reprocessing of an existing PDF document.
As a general solution, I create a new PDF, page by page, import the elements that I want to lay up, then add additional text or graphics.
#!/usr/bin/perl
use warnings; use strict;
use PDF::API2;
my $infile = shift (@ARGV);
my $outfile = shift (@ARGV);
die "usage $0: infile outfile"
unless $infile && $outfile;
my $pdf_in = PDF::API2->open($infile);
my $pdf_out = PDF::API2->new;
foreach my $pagenum (1 .. $pdf_in->pages) {
my $page_in = $pdf_in->openpage($pagenum);
#
# create a new page
#
my $page_out = $pdf_out->page(0);
my @mbox = $page_in->get_mediabox;
$page_out->mediabox(@mbox);
my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum);
#
# lay up the input page in the output page
# note that you can adjust the position and scale, if required
#
my $gfx = $page_out->gfx;
$gfx->formimage($xo,
0, 0, # x y
1); # scale
#
# add page number text
#
my $txt = $page_out->text;
$txt->strokecolor('#000000');
$txt->translate(
my $_x = 200,
my $_y = 50
);
my $font = $pdf_out->corefont('Courier');
$txt->font($font, 12);
$txt->text( 'Page: '.$pagenum );
#
# add header image
#
my $header_img = $pdf_out->image_png('SomeHeader.png');
$gfx->image($header_img, 0, 400);
}
$pdf_out->saveas($outfile);
Have a look at PDF::API2::Simple
instead. This CPAN modules provides some handy helper methods around PDF::API2
including headers and footers.
Here is a simple working header/footer example:
use 5.012;
use warnings;
use PDF::API2::Simple;
our $PageNo;
my $pdf = PDF::API2::Simple->new(
file => 'file.pdf',
header => \&header,
footer => \&footer,
);
$pdf->add_font('Verdana');
for my $page (1..3) {
$pdf->add_page;
$pdf->image( 'image.png', x => 300, y => 300 );
}
$pdf->save;
sub header { shift->text( 'Header text here' ) }
sub footer { shift->text( 'page: ' . ++$PageNo, x => 10, y => 10 ) }
/I3az/
I've looked online for details about using PDF::AP12, but details are very sparce. Looked on the CPAN site, but that gives no indication of where to read docs, so where do you find details? (At present, CPAN command line won't even recognise PDF::AP12 =or AP13= as legit modules! and not quite got to grips with downloading tar ball and installing manually into strawberry perl)
I'm curious about this "importPageIntoForm" item: If I designed a master PDF page using a DTP program, (for label production ie 3 wide by 8 deep) and put a dummy graphic (?) on the right of each cell, would I be able to swap these out using this? (if you like, an online "mail merge" replacing the graphics with individual QR codes c/o GD::BARCODE::QRcode)
Or would it be best to create each page from scratch with the main program?