Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^10: Making PDF Tables

by poj (Abbot)
on Nov 08, 2014 at 13:54 UTC ( [id://1106562]=note: print w/replies, xml ) Need Help??


in reply to Re^9: Making PDF Tables
in thread Making PDF Tables

See my reply here Re^9: Making PDF Tables.

Try this

#!perl use strict; use warnings; use PDF::API2; use PDF::Table; use pdfdata; # #A4 Landscape my $WIDTH = 842; my $HEIGHT = 595; my $BORDER_LEFT = 50; my $BORDER_RIGHT = 100; my $BORDER_TOP = 100; my $BORDER_BOTTOM = 50; my $TABLE_SPACING = 30; my $pdf = new PDF::API2(-file => "tables_side_by_side_test2.pdf"); $pdf->mediabox($WIDTH, $HEIGHT); my %font = ( 'H' => $pdf->corefont("Helvetica", -encoding => "utf8"), 'HB'=> $pdf->corefont("Helvetica-Bold", -encoding => "utf8"), ); my $target_page_no; my $page_no = 0; my $page = new_page(); my $x = $BORDER_LEFT; my $y = $HEIGHT-$BORDER_TOP; my $w = $WIDTH-$BORDER_LEFT-$BORDER_RIGHT; # first table ($page,undef,$y) = create_table($page,pdfdata::_ob_disc,"The First Tab +le"); table_space($TABLE_SPACING); # second table ($page,undef,$y) = create_table($page,pdfdata::_excluded,"The Second T +able"); table_space($TABLE_SPACING); # block tables my ($acc,$accA,$accB) = pdfdata::acc; # data # print 3 tables for each block for my $i (1..@$acc){ $x = $BORDER_LEFT; $w = 650; # table acc ($page,undef,$y) = create_table($page,$acc->[$i-1],"Block $i"); table_space($TABLE_SPACING/2); my $block_page_no = $pdf->pages; # table A $w = 180; $target_page_no = $block_page_no; my ($pageA,$spanA,$yA) = create_table($page,$accA->[$i-1],"Table A : + $i"); # table B $x = 260; $w = 440; $target_page_no = $block_page_no; my ($pageB,$spanB,$yB) = create_table($page,$accB->[$i-1],"Table B $ +i"); # cal start of next block if ($spanA > $spanB){ $page = $pageA; $y = $yA } elsif ($spanB > $spanA){ $page = $pageB; $y = $yB; } else { $page = $pageA; $y = ($yA < $yB) ? $yA : $yB; } table_space($TABLE_SPACING); } $pdf->saveas(); # create table sub create_table{ my ($page,$data,$title) = @_; # title my $caption = $page->text(); $caption->font($font{'HB'},10); $caption->translate($x,$y+1); $caption->text($title); my $pdftable = new PDF::Table; my ($next_page, $span, $new_y) = $pdftable->table( $pdf, $page, $data, x => $x, w => $w, start_y => $y, next_y => $HEIGHT-$BORDER_TOP, start_h => $y, next_h => $HEIGHT-$BORDER_TOP, new_page_func => \&new_page, padding => 2, padding_right => 10, border => 0, background_color_odd => "#E0E0E0", background_color_even => "#FFFFFF", header_props => { font => $font{'HB'}, font_size => 10, font_color => '#000000', bg_color => '#FFFFFF', repeat => 1, justify => 'center', }, column_props => [ map{ { justify => 'center', font => $font{'H'}, font_size => 9, } }1..@{$data->[0]}], ); return ($next_page,$span,$new_y); }; # create new page sub new_page { ++$target_page_no; if ($target_page_no <= $page_no){ return $pdf->openpage($target_page_no); } # new page my $page = $pdf->page; ++$page_no; # page numbers my $g = $page->gfx(); $g->textlabel($WIDTH-30,15,$font{'H'},8, "Page $page_no", -color => '#808080', -align => 'right', ); # title $g->textlabel( 416, 566,$font{'HB'}, 11, "Test Tables PDF", -color => '#000000', -align => 'center', ); # line $g->linewidth(1); my $x = $BORDER_LEFT; my $y = $HEIGHT-$BORDER_TOP+15; $g->move($x,$y); $g->line($WIDTH-$x,$y); $g->stroke; return $page; } # create space between tables sub table_space{ $y -= shift; if ($y < $BORDER_BOTTOM){ $y = $HEIGHT-$BORDER_TOP; $page = new_page(); } }

I have moved your test data into this separate module pdfdata.pm which should make it easy to switch to real data.

package pdfdata; sub acc { my @acc=(); my $hdr = ['Acc #', 'Name', 'OB Date', 'Number']; my @rows = (['12345','Carl Sea','03/13/2014','4'], ['764539','Deon gomes','11/07/2013','2'] ); push @acc,[$hdr,$_] for @rows; my @accA=(); my $hdrA = ["Tie #", "Year", "Model"]; my @rowsA = ( [ ["1", "2000", "OLD",], ["2", "2001", "OLD",], ["3", "2002", "OLD",], ["4", "2003", "OLD",], ["5", "2004", "OLD",], ["6", "2005", "OLD",], ["7", "2011", "NEW",], ],[ ["A", "2000", "OLD",], ["B", "2001", "OLD",], ["C", "2002", "OLD",], ["D", "2003", "OLD",], ["E", "2004", "OLD",], ["F", "2005", "OLD",], ["G", "2011", "NEW",], ],); push @accA,[$hdrA,@$_] for @rowsA; my @accB=(); my $hdrB = ["Name #","All", "DXS", "Delst","A #1","B #2", "C #3", "D + #4"]; my @rowsB = ( [ ["1","Stephen King", "5/5/1930", "N","O","P", "-", "-",], ["2","Charles Xing", "6/28/1550", "N","P","O", "-", "-",], ["3","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["4","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["5","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["6","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["7","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["8","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ],[ ["A","Maru Louey", "5/5/1950", "N","O","P", "-", "-",], ["B","Gome Thing", "6/28/1950", "N","P","O", "-", "-",], ["C","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["D","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["E","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["F","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["G","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ["H","Dnieal Cruz", "07/25/1988", "N","O","E", "-", "-",], ],); push @accB,[$hdrB,@$_] for @rowsB; return \@acc,\@accA,\@accB; } # This is simulating random rows of data coming from the database sub _ob_disc { my @disc = ( ["A Number", "Name", "Date", "Reference"], ["23456", "Aname Another", "01/11/2014", "00007"], ["23456", "jhjhj ssaaa", "01/11/2014", "00007"], ["23456", "Loren asddf", "01/11/2014", "00007"], ["23456", "Serrt jahft", "01/11/2014", "00007"], ["23456", "Ojshfg treiu", "01/11/2014", "00007"], ["23456", "Rssskjj tehrrr", "01/11/2014", "00007"], ["23456", "Maru Smith", "01/11/2014", "00007"], ["23456", "Joe A Smith", "01/11/2014", "00007"], ["23456", "Marck Del", "01/11/2014", "00007"], ["23456", "Gujjnn Theeee", "01/11/2014", "00007"], ["23456", "Mary Lou", "01/11/2014", "00007"], ["23456", "Jackob Joen", "01/11/2014", "00007"], ["23456", "Marcouys De Larent", "01/11/2014", "00007"], ["23456", "Once Uon Time", "01/11/2014", "00007"], ["23456", "Nelson Mkkjhtt", "01/11/2014", "00007"], ); return \@disc; }; sub _excluded { my @exc = ( ["Acc #", "Date", "Name", "Name #", "Others", "SSW", "XXXS", "#1", +"#2" , "#3","#4",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ["12345", "11/01/2014", "consequat quis", "3", "Joe Doe", "03/07/20 +14", "0", "O", "E" , "P","",], ); return \@exc; }; 1;
poj

Replies are listed 'Best First'.
Re^11: Making PDF Tables
by Anonymous Monk on Nov 11, 2014 at 14:21 UTC
    Hi there!
    I thought it was a lost cause, but thanks, with what I have and what you posted I can make this work the way I need it for now. I wish this module would have the capabilities to do colspan, it would be very handy. But, like you mentioned, it can get complicated once you start placing tables side by side, even with all this I still like PERL for it versus JAVA.
    Thanks again!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1106562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found