5

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 );
Space
  • 7,049
  • 6
  • 49
  • 68

3 Answers3

5

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;
}
daxim
  • 39,270
  • 4
  • 65
  • 132
  • Thanks Daxim: this code is to edit the existing document. Can you please suggest me how can i use it to create new doc with images – Space Jun 04 '10 at 13:02
  • Use the [`new` constructor](http://p3rl.org/PDF::API2#GENERIC_METHODS). Please do read the docs. – daxim Jun 04 '10 at 15:04
  • Thanks daxim: Can you please also suggest me how can i add properties to pdf doc. – Space Jun 09 '10 at 07:08
  • Open a new question for your new question: http://stackoverflow.com/questions/ask – daxim Jun 09 '10 at 10:37
4

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);
dwarring
  • 4,794
  • 1
  • 26
  • 38
  • This is a really great approach - rather than modifying the pdf in place, using it as a template to create a new pdf. Thanks for telling us about importPageIntoForm()! – Mark Aug 31 '11 at 09:24
2

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/

draegtun
  • 22,441
  • 5
  • 48
  • 71