Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add header, footer with images using PDF::API2::Lite?

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 );
like image 527
Space Avatar asked Jun 01 '10 07:06

Space


2 Answers

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;
}
like image 140
daxim Avatar answered Sep 19 '22 18:09

daxim


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);
like image 38
dwarring Avatar answered Sep 20 '22 18:09

dwarring