Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

poj's scratchpad

by poj (Vicar)
on Jun 12, 2013 at 17:16 UTC ( #1038520=scratchpad: print w/ replies, xml ) Need Help??

change in make_table
if ( $args{'title'} ){ my $caption = $page->text(); $caption->font($font{'HB'},10); $caption->translate($args{x},$args{y}+1); $caption->text( $args{'title'} ); $args{y} -= 5; } . . . . start_y => $args{y}, ============================================
#!/usr/bin/perl =head1 VERSION 2 real data in TestData excluded changed to executed 3 added @block 4 changed to %block, added target_page logic and block_limit =cut use strict; use warnings; use PDF::API2; use PDF::Table; use Data::Dump 'pp'; use RealData; my $DEBUG = 1; if ($DEBUG){ open DBUG,'>','run.log' or die "$!"; _log("Start.."); } my %font=(); my %layout=(); init_layout(\%layout); my $page_no; my $target_page_no; my $just_data = RealData::collect_data(); pp $just_data; processing_data($just_data); close DBUG if $DEBUG; exit; sub processing_data { my $just_data = shift; foreach my $member_number ( keys %{$just_data} ) { my $data = $just_data->{$member_number}; add_headers($data); _log("Creating pdf for $member_number"); #_log(pp $data); my $pdf = new PDF::API2(-file => "c:/temp/pdf/".$member_number.".p +df"); $pdf->mediabox($layout{page_width}, $layout{page_height}); init_fonts($pdf); $page_no = 0; my $page = new_page( pdf => $pdf ); # remove duplicates #my @data=(); my %seen=(); #for (@{$data->{multi}}){ # push @data,[@$_] ;#unless $seen{$_->[0]}++; #} # sort on name #my $head = shift @data; #@data = sort { $a->[1] cmp $b->[1] } @data; #unshift @data,$head; # multi Table my $y = $layout{top}; _log("Table 1 page $page_no y = $y"); ($page,undef,$y) = make_table( pdf => $pdf, page => $page, data => $data->{multi}, title => "Sub Header for 1a - multi", x => $layout{border_left}, y => $y, w => $layout{width}, ); ($page,$y) = table_space( pdf => $pdf, page => $page, y => $y, space => $layout{table_spacing}, ); # executed table _log("Table 2 page $page_no y = $y"); ($page, undef, $y) = make_table( pdf => $pdf, page => $page, data => $data->{executed}, title => "Sub Header for Header 2 - executed", x => $layout{border_left}, y => $y, w => $layout{width}, ); ($page,$y) = table_space( pdf => $pdf, page => $page, y => $y, space => $layout{table_spacing}, ); my %block=(); my %seen=(); for my $i ( 0 .. $#{ $data->{princ} } ){ my @f = @{$data->{princ}[$i]}; push @{$block{$f[0]}{'princ'}} ,[ @f ] unless $seen{$f[0]}++; push @{$block{$f[0]}{'type'}} ,[ @{$data->{type}[$i]} ]; push @{$block{$f[0]}{'person'}},[ @{$data->{person}[$i]} ]; } # headers my $head3 = ["Head 3a", "Head 3b", "Head 3c", "Head 3d"]; my $head4 = ["Head 4a", "Head 4b", "Head 4c",]; my $head5 = ["Head 5a","Head 5b", "Head 5c", "Head 5d","Head 5e","Head 5f", "Head 5g", "Head 5h",]; # print 3 tables for each block #for my $i ( 0 .. $#{ $data->{princ} } ){ for my $key (sort keys %block){ # start new page if block start near bottom if ($y < $layout{block_limit}){ $page = new_page( pdf => $pdf ); $y = $layout{top}; } _log("\nTable 3 page $page_no y = $y"); # princ table ($page,undef,$y) = make_table( pdf => $pdf, page => $page, data => [$head3,@{$block{$key}{princ}}],#$data->{princ}, title => "Header 3 - Princ", x => $layout{border_left}, y => $y, w => $layout{width}, ); ($page,$y) = table_space( pdf => $pdf, page => $page, y => $y, space => $layout{table_spacing}/2, ); my $block_page_no = $pdf->pages; # type table _log("Table 4 page $page_no y = $y"); $target_page_no = $block_page_no; my ($pageA,$spanA,$yA) = make_table( pdf => $pdf, page => $page, data => [$head4,@{$block{$key}{type}}],#$data->{type}, title => "Header 4 - Type", x => $layout{border_left}, y => $y, w => 200, ); _log("Table 4 returns page $page_no span = $spanA y1 = $yA"); # person table _log("Table 5 page $page_no y = $y"); $target_page_no = $block_page_no; my ($pageB, $spanB, $yB) = make_table( pdf => $pdf, page => $page, data => [$head5,@{$block{$key}{person}}],#$data->{person}, title => "Header 5 - Person", x => $layout{border_left} + 210, y => $y, w => $layout{width} - 210, ); _log("Table 5 returns page $page_no span = $spanA y1 = $yB"); # decide which table was longest # to find 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; } ($page,$y) = table_space( pdf => $pdf, page => $page, y => $y, space => $layout{table_spacing}, ); } $pdf->save; $pdf->end(); } } # End of sub # Make tables sub make_table{ my (%args) = @_; my $pdf = $args{pdf}; my $page = $args{page}; # start new page if near bottom if ($args{y} < $layout{start_limit}){ $page = new_page( pdf => $pdf ); $args{y} = $layout{top}; } # title my $caption = $page->text(); $caption->font($font{'HB'},10); $caption->translate($args{x},$args{y}+1); $caption->text( $args{'title'} ); # table my $pdftable = new PDF::Table; my ($next_page, $span, $new_y) = $pdftable->table( $pdf, $page, $args{data}, x => $args{'x'}, w => $args{'w'}, start_y => $args{y} - 5, next_y => $layout{top}, start_h => $args{y} - $layout{border_bottom}, next_h => $layout{height}, new_page_func => sub{ new_page(pdf=>$pdf) }, padding => 2, padding_right => 10, border => 1, background_color_odd => "#FFFFFF", background_color_even => "#E0E0E0", 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 => 10, #} }1..@{$args{data}->[0]}], ); return ($next_page, $span, $new_y); }; # create new page sub new_page { my (%args) = @_; my $pdf = $args{ pdf }; # only create a new page if target # is greater than last existing page no ++$target_page_no; if ($target_page_no <= $page_no){ return $pdf->openpage($target_page_no); } # new page my $page = $pdf->page; ++$page_no; _log("\n*** Created page $page_no ***"); # page logo #my $logo = "images/logo.jpg"; #my $image = $pdf->image_jpeg($logo, 122, 35); #my $gfx = $page->gfx; #$gfx->image($image, 5, 538, 230, 57); # logo position and size on p +age # page numbers my $g = $page->gfx(); $g->textlabel( $layout{right},25,$font{'H'},8, "Page $page_no", -color => '#808080', -align => 'right', ); doc_title( pdf => $pdf, page => $page ); month_doc( pdf => $pdf, page => $page ); ind_inform( pdf => $pdf, page => $page ); # line $g->linewidth(1); my $x = $layout{border_left}; my $y = $layout{top} + 15; $g->move($x,$y); $g->line($layout{right},$y); $g->stroke; return $page; } # create space between tables sub table_space{ my (%args) = @_; my $page = $args{ page }; my $pdf = $args{ pdf }; my $y = $args{y} - $args{space}; if ($y < $layout{border_bottom}){ $y = $layout{top}; $page = new_page( pdf => $pdf ); } return ($page,$y); } sub doc_title { my (%args) = @_; my $pdf = $args{pdf}; my $page = $args{page}; my %opt = ( -color => '#000000', -align => 'center' ); $page->gfx->textlabel( 416, 566, $font{'HB'}, 11, "My Document Name Here", %opt ); + $page->gfx->textlabel( 416, 553, $font{'H'}, 10, "More Doc title Here", %opt ); $page->gfx->textlabel( 416, 543, $font{'H'}, 9, "100 Main street", %opt ); } sub month_doc { my (%args) = @_; my $pdf = $args{pdf}; my $page = $args{page}; my $date = get_date(); my %opt = ( -color => '#000000', -align => 'right' ); my $x = $layout{right}; my $y = $layout{page_height} - 30; $page->gfx->textlabel( $x-37,$y, $font{'H'}, 8, "For the Month: ", %opt ); $page->gfx->textlabel( $x,$y, $font{'HB'}, 8, $date, %opt ); $page->gfx->textlabel( $x-37,$y-10, $font{'H'}, 8, "Doc Number: ", %opt ); $page->gfx->textlabel( $x,$y-10, $font{'HB'}, 8, "0000007", %opt ); } sub ind_inform { my (%args) = @_; my $pdf = $args{pdf}; my $page = $args{page}; my %opt = ( -color => '#000000', -align => 'center' ); $page->gfx->textlabel( 416, 520, $font{'HB'}, 12, "More title info here", %opt ); $page->gfx->textlabel( 116, 520, $font{'HB'}, 12, "Member Number: xx000", %opt ); + } sub get_date { my ($mon, $mday, $year) = (localtime)[4,3,5]; return sprintf '%02d-%02d-%04d', $mon+1, $mday, $year+1900; } sub add_headers { my $hr = shift; unshift @{$hr->{multi}},["Header 1a", "Header 1b", "Header 1c", "Hea +der 1d"]; unshift @{$hr->{executed}},["Header 2a","Header 2b","Header 2c","Hea +der 2d", "Header 2e","Header 2f","Header 2g","Header 2h", "Header 2i","Header 2j","Header 2k"]; # unshift @{$hr->{princ}},["Head 3a", "Head 3b", "Head 3c", "Head 3d" +]; # unshift @{$hr->{type}},["Head 4a", "Head 4b", "Head 4c",]; # unshift @{$hr->{person}},["Head 5a","Head 5b", "Head 5c", # "Head 5d","Head 5e","Head 5f", "Head 5g", "Head 5h",]; } sub init_layout { my $hr = shift; # landscape $hr->{page_width} = 785; $hr->{page_height} = 590; $hr->{border_left} = 50; $hr->{border_right} = 50; $hr->{border_top} = 100; $hr->{border_bottom} = 50; $hr->{table_spacing} = 50; $hr->{top} = $hr->{page_height} - $hr->{border_top}; $hr->{right} = $hr->{page_width} - $hr->{border_right}; $hr->{width} = $hr->{right} - $hr->{border_left}; $hr->{height} = $hr->{top} - $hr->{border_bottom}; $hr->{start_limit} = $hr->{border_bottom} + 50; $hr->{block_limit} = $hr->{border_bottom} + 200; } sub init_fonts { my $pdf = shift; %font = ( 'H' => $pdf->corefont("Helvetica", -encoding => "utf8"), 'HB'=> $pdf->corefont("Helvetica-Bold", -encoding => "utf8"), ); } sub _log { my $line = shift; my $time = sprintf "%02d:%02d:%02d",(localtime)[2,1,0]; print DBUG "$time : $line\n"; print "$line\n" if $DEBUG; }
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2015-08-30 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (348 votes), past polls