#!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 Table"); table_space($TABLE_SPACING); # second table ($page,undef,$y) = create_table($page,pdfdata::_excluded,"The Second Table"); 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(); } }