in reply to Re: Looping in PDF::Table module
in thread Looping in PDF::Table module

Hi, Thanks a lot for your advises, i do learn from it. I only got one problem now, how to make a dynamic pdf file. I mean the table height and the start position where the next table will be written by the program because in pdf::table module, i need to specify the x and y coordinates on where should the program start to write the table in pdf.

Replies are listed 'Best First'.
Re^3: Looping in PDF::Table module
by Eliya (Vicar) on Jul 25, 2013 at 14:07 UTC

    The table() method returns 3 values:

    1. PDF::API2::Page object instance that the table ends on
    2. count of pages that the table spans
    3. y position of the table bottom

    With that info (in particular 1 and 3) you can compute where to place the next table.  Sample code:

    #!/usr/bin/perl -w use strict; use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "test.pdf"); my $page = $pdf->page; # some sample data (3 tables) my @data; for my $t (1..3) { my $table = []; for my $r (1..50) { push @$table, [ map "Table $t : $_$r", 'A'..'E' ]; } push @data, $table; } my $page_height = 700; my $top_y = 750; my $y = $top_y; for my $table (@data) { my ($p_last, undef, $y_bot) = $pdftable->table( $pdf, $page, $table, x => 50, w => 500, start_y => $y, next_y => $top_y, start_h => $page_height - ($top_y-$y), next_h => $page_height, # some optional params padding => 5, padding_right => 10, background_color_odd => "#eee", background_color_even => "#ddd", ); $page = $p_last; $y = $y_bot - 30; # vertical distance between tables if ($y < 50) { # page wrap $page = $pdf->page; $y = $top_y; } } $pdf->saveas();
Re^3: Looping in PDF::Table module
by rnewsham (Curate) on Jul 25, 2013 at 07:12 UTC

    The properties x, start_y, next_y, start_h, next_h define the coordinates and size of the table. The start_y and start_h set the table position and height on the first page of the pdf. If there is more data than can be displayed on the first page it will continue on further pages with the y coordinates and height defined by next_y and next_h.

    Here is an example that I extracted from one of my working programs, I have trimmed it down to remove irrelevant code so it is not a complete working example but should give you an idea.

    use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => 'reports/report.pdf'); $pdf->mediabox('A4'); my $page = $pdf->page; # define header properties my $hdr_props = { # This param could be a pdf core font or user specified TTF. # See PDF::API2 FONT METHODS for more information font => $pdf->corefont("Times", -encoding => "utf8"), font_size => 10, font_color => '#555555', bg_color => 'grey', repeat => 1, # 1/0 eq On/Off if the header row should be repea +ted to every new page }; # build the table layout $pdftable->table( # required params $pdf, $page, \@pdf_report_data, # report data in an array of hashes x => 50, w => 495, start_y => 755, # sets the y position of the table on the firs +t page next_y => 775, # sets the y position of tables on subsequent +pages start_h => 730, # sets the height of the table on the first pa +ge next_h => 750, # sets the height of the table on subsequent p +ages # some optional params padding => 5, padding_right => 10, background_color_odd => "white", # cell background colour f +or odd rows background_color_even => "lightgrey", # cell background colour f +or even rows header_props => $hdr_props, # loads the header properties as d +efined above ); # do other stuff with $pdf # save pdf $pdf->saveas();
      Thank you for your reply rnewsham, i get your point. Sorry for my last post, i wasn't able to fully explain it. My problems occur because i created more than 1 table and i make sure that the second and so on tables looks attached to the end of the first table/table before the next table. I did this because i can't merge columns and rows using pdf::table module. Is it possible to merge columns and rows using pdf::table? if there is a way to do it, it will save me a lot from all the troubles i am facing right now.

        It does not appear to be a documented feature and a quick skim of the modules source and I can't see anything to suggest such a capability.

        You have raised an interesting issue that may slow my own adoption of PDF::Table. It looks like you will need to contact the authors and see if the functionality could be added. The code does not look particularly complicated so if they don't add it by the time I need it I will do it myself, unfortunately I have no need at the moment and more urgent matters need my attention so it won't be anytime soon.