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; }

In reply to Re: Perl PDF format issue. by almut
in thread Perl PDF format issue. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.