Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I need to create a PDF document with a few tables in it, from the code sample of the module PDF::Table its simple to have one table in the same page of a PDF, but I can’t figure it out how I would add another table into the same PDF page. Has any of you tried or had to do the same thing using the same module?
Code Sample
use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "table_of_lorem.pdf"); my $page = $pdf->page; # some data to layout my $some_data =[ ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequat quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit."], #... and so on ]; $left_edge_of_table = 50; # build the table layout $pdftable->table( # required params $pdf, $page, $some_data, x => $left_edge_of_table, w => 495, start_y => 750, next_y => 700, start_h => 300, next_h => 500, # some optional params padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", #cell background color for +even rows ); # second table in the same PDF page $pdftable->table( # required params $pdf, $page, $some_data, x => $left_edge_of_table, w => 500, start_y => 755, next_y => 705, start_h => 305, next_h => 505, # some optional params padding => 5, padding_right => 10, background_color_odd => "navy", background_color_even => "red", #cell background color for even r +ows ); # do other stuff with $pdf $pdf->saveas();

Thanks for looking!

Replies are listed 'Best First'.
Re: Making PDF Tables
by Loops (Curate) on Oct 24, 2014 at 20:41 UTC

    It's quite ugly but PDF::Table is actually consuming the contents of $some_data by shifting each element out of it for display. This means that when you call table the second time you're passing in an empty data set which causes a divide by zero error. You'll have to make a copy of the data before passing it in.

    As an aside, you'll want to record the third parameter returned from the first table call. This is the last y value used in creating the table. Subtract a few from what you get back (origin is the bottom of the page, not the top) and use it as the starting position of the second table.

    use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "table_of_lorem.pdf"); my $page = $pdf->page; my $some_data = [ ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ]; my $left_edge_of_table = 50; my @tmp = @$some_data; my (undef, undef, $boty) = $pdftable->table( $pdf, $page, \@tmp, x => $left_edge_of_table, w => 495, start_y => 750, next_y => 700, start_h => 300, next_h => 500, padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", ); @tmp = @$some_data; $pdftable->table( $pdf, $page, \@tmp, x => $left_edge_of_table, w => 500, start_y => $boty - 10, next_y => 705, start_h => 305, next_h => 505, padding => 5, padding_right => 10, background_color_odd => "navy", background_color_even => "red", #cell background color for even r +ows ); $pdf->saveas();
      That works, but the problem is when the data for the first table grows like in this situation:
      my $some_data = [ ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ]; my $more_data = [ ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequa +t quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit +."], ]; ... #@tmp = @$some_data; @tmp = @$more_data; ...

      It will overlap the second table.
        Increase the gap between the tables, 70 seems about right
        start_y => $boty - 70,
        Update : If that doesn't work, try increasing the start_h, next_h parameters and use $nextpage for the second table.
        my ($nextpage, undef, $boty) = $pdftable->table( $pdf, $page, $some_data, x => $left_edge_of_table, w => 495, start_y => 750, next_y => 750, start_h => 750, next_h => 750, padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", ); $pdftable->table( $pdf, $nextpage, $more_data, x => $left_edge_of_table, w => 500, start_y => $boty - 10, next_y => 750, start_h => $boty - 10, next_h => 750, padding => 5, padding_right => 10, background_color_odd => "navy", background_color_even => "red", #cell background color for even r +ows );
        poj
Re: Making PDF Tables
by mhearse (Chaplain) on Oct 24, 2014 at 19:55 UTC
    About 6 years ago I used PDF::Template to generate tables in PDF documents: Dynamic tables using PDF::Template

    I was very pleased with the results. In the end it was CGI app which generated PDF tables from SQL queries.

      yes but doesnt PDF::Template require pdflib_pl which is not free for production purposes ?
      thats some sort of a letdown IMHO.
      The temporal difficulty with perl is u need to know C well to know it's real nature.else u just keep *using* it and writing inefficient code