in reply to Perl PDF format issue.

align the text inside the $head_data to the center of the page

The idea would be to use the table option column_props (see "COLUMN PROPERTIES" in the docs, for details):

column_props => [ { justify => 'center' } ],

(one hash per column as an array — you only need one entry, though, as your headers/footers don't have more than one column)

Unfortunately, PDF::Table does currently not allow centering of cell content.  If you look in the source you'll see "#TODO: Implement Center text align"...  Time for another patch.  Locate this piece of code (PDF/Table.pm, line 556)

if($col_props->[$j]->{justify} eq 'right') { $space = $calc_column_widths->[$j] -($txt->advancewidth($r +ecord->[$j]) + $pad_right); }

and extend it to read

if($col_props->[$j]->{justify} eq 'right') { $space = $calc_column_widths->[$j] -($txt->advancewidth($r +ecord->[$j]) + $pad_right); } elsif ($col_props->[$j]->{justify} eq 'center') { $space = ($calc_column_widths->[$j] -$txt->advancewidth($r +ecord->[$j])) / 2; }

This will make the column_props definition work with "center", too.


make this header text to show on every pdf page

That's an easier one.  Just wrap your header and footer code into subroutines, and call them from within your newpage() function (which is being called for every page initialisation)

sub newpage { my $page = $pdf->page; $page->mediabox(792,612); header($page); footer($page); page_nr($page, $page_num++); return $page; }

In case you also want to show the table column headers on every page, just specify, for the main table  (see "HEADER ROW PROPERTIES" in the docs):

header_props => { repeat => 1 },

add page numbers

As hinted at above, a page number routine would also be called from within newpage(). Its implementation could look like this (using PDF::API2's textlabel() method — but you could also use PDF::Table's text_block() method)

$page->gfx->textlabel( # PDF::API2 method 760, 15, # x, y $pdf->corefont("Helvetica"), 11, # font, size "Page $num", # text -color => '#808080', -align => 'right', );

The entire code would then look as shown below, which produces this PDF.

(BTW, just in case you came here via Google, you might also want to read Part One).

#!/usr/bin/perl use strict; use warnings; use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "pdf_test.pdf"); $pdf->preferences( -thumbs => 1, ); my $page_num = 1; # some data to layout my $some_data =[ ["Header 1", "Header 2", "Header 3", "Header 4", "Header 5" ], # content, 50 rows x 5 columns map { my $row = $_; [ map "Content [$row, $_]", 1..5 ] } 1..50, ]; main_table($pdf, $some_data); $pdf->saveas(); ############## sub main_table { my $pdf = shift; my $data = shift; my $page = newpage(); # first page # build the table layout $pdftable->table( # required params $pdf, $page, $data, x => 70, w => 650, start_y => 527, # 550 next_y => 527, start_h => 500, next_h => 500, # some optional params padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", #cell background color f +or even rows new_page_func => \&newpage, # as defined above cell_render_hook => sub { # this is our newly introduced ca +llback function my ($page, $row, $col, $x, $y, $w, $h) = @_; if ($col == 0) { # do nothing except for first column my $url = "http://www.anywhere.com?row=$row"; my $annot = $page->annotation(); $annot->url( $url, -rect => [$x, $y, $x+$w, $y+$h], -border => [1,1,1] ); } }, header_props => { repeat => 1 }, ); } sub newpage { my $page = $pdf->page; $page->mediabox(792,612); header($page); footer($page); page_nr($page, $page_num++); return $page; } sub header { my $page = shift; # we need a page to place ourselves on my $head_data =[ ["My Company Name",], ["List of Users",], ["Welcome User 1",] ]; $pdftable->table( # required params $pdf, $page, $head_data, x => 20, w => 750, start_y => 594, # top od doc on page - 594 next_y => 594, start_h => 100, next_h => 100, #OPTIONAL PARAMS BELOW #max_word_length=> 20, # add a space after every 20th symbo +l in long words like serial numbers padding => 2, # cell padding #padding_top => 1, # top cell padding, overides padding padding_right => 10, # right cell padding, overides paddi +ng #padding_left => 5, # left cell padding, overides paddin +g #padding_bottom => 5, # bottom padding, overides -padding border => 0, # border width, default 1, use 0 for + no border border_color => 'navy', # default black #font => $pdf->corefont("Helvetica", -encoding => " +utf8"), # default font #font_size => 10, #font_color_odd => 'black', #font_color_even=> 'black', #background_color_odd => "#ffffff", #background_color_even => "#ffffff", #cell background color f +or even rows #header_props => $hdr_props, column_props => [ { justify => 'center' } ], ); } sub footer { my $page = shift; # we need a page to place ourselves on my $footer_data =[ [ get_date() ] ]; $pdftable->table( # required params $pdf, $page, $footer_data, x => 20, w => 750, start_y => 30, # top od doc on page next_y => 30, start_h => 30, next_h => 30, #OPTIONAL PARAMS BELOW #max_word_length=> 20, # add a space after every 20th symbo +l in long words like serial numbers padding => 5, # cell padding border => 0, # border width, default 1, use 0 for + no border border_color => 'red', # default black font => $pdf->corefont("Helvetica", -encoding => "u +tf8"), # default font font_size => 9, #header_props => $hdr_props, column_props => [ { justify => 'center' } ], ); } sub page_nr { my $page = shift; my $num = shift; $page->gfx->textlabel( # PDF::API2 method 760, 15, # x, y $pdf->corefont("Helvetica"), 11, # font, size "Page $num", # text -color => '#808080', -align => 'right', ); } sub get_date { my ($mon, $mday, $year) = (localtime)[4,3,5]; return sprintf '%02d-%02d-%04d', $mon+1, $mday, $year+1900; }

Replies are listed 'Best First'.
Re^2: Perl PDF format issue.
by Anonymous Monk on Apr 07, 2009 at 21:55 UTC
    That's a big improvement, but any idea why when I am opening the PDF file it is giving me an error saying
    "Illegal Operation inside a path". Did you try it, did you get the same error?

      I can view it fine with several versions of acroread, xpdf, kpdf and evince (all on Linux).  But you don't seem to be the first one to have encountered that error...  In theory, the problem could be either with the PDF format (as generated by PDF::API2), or with the the viewer software, which might well have some subtle bug.  I'm afraid I can't help more.  Maybe try a different viewer?

        I tried on Acrobat 7.0 which gave me this error, but using a newer version of acrobat 8.0 didn't give me the error. I guess updating should help.