views:

439

answers:

3

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 );
+3  A: 

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
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
Use the [`new` constructor](http://p3rl.org/PDF::API2#GENERIC_METHODS). Please do read the docs.
daxim
Thanks daxim: Can you please also suggest me how can i add properties to pdf doc.
Space
Open a new question for your new question: http://stackoverflow.com/questions/ask
daxim
+1  A: 

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
+1  A: 

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 graphics.

#!/usr/bin/perl
use common::sense;

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 ajust the size 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);
snoopy