Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Colspan in PDF Help!

by Anonymous Monk
on Nov 26, 2012 at 20:05 UTC ( #1005755=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I need some help on trying to do a colspan on this PDF table project I have. Here on this sample code I can explain what I am tring to do. I need to make the rows with "Primary Info" and "Secondary Info" to do a "colspan". I am also trying to make the totals "line" to stretch (colspan) over the table to create a line to divide the sections on this table. Maybe I could pass an image to simulate the colspan, but can't figure it out how to pass the image code through the data string. I looked everywhere and I can't find any solutions for it, maybe someone here did this before and could show me how I can get his done.
Here is the sample code and thanks for the help in advance!
#!/usr/bin/perl use warnings; use strict; use PDF::API2; use PDF::Table; use Data::Dumper; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "report.pdf"); my $page = $pdf->page(); $pdf->mediabox('A4'); # some data to layout my $some_data =[ ['Name', 'Address', 'Account', 'Amount',], ['','','','',], ['Primary Info','','','',], ['','','','',], ['1 - Charles Dawrwin', '1 - Boston, MA', '1 - 123456789', + '150.00'], ['2 - Arnold Shuwerts', '2 - San Rose, CA', '2 - 844756485' +, '250.00'], ['3 - Joe Doe', '3 - Revere, MA', '3 - 000034559', + '100.00'], ['4 - Mary Loo', '4 - New York, NY', '4 - 333449687', + '125.00'], ['','','','',], ['','','','___________',], ['','Primary Sub Total','','550.00',], ['','','','',], ['1a - Mary Dawrwin', '1a - Cambrigde, MA', '1a - 12345278 +9', '550.00'], ['2a - Joe Doark', '2a - Miami, FL', '2a - 84472648 +5' , '350.00'], ['','','','',], ['Secondary Info','','','',], ['','','','',], ['5 - John Dell', '5 - Portland, ME', '5 - 111000384', + '55.00'], ['6 - Mary Loumain', '6 - Cambridge, MA','6 - 000034569', +'1,000.00'], ['7 - Charles Town', '7 - New Port, NH', '7 - 222299944', + '200.00'], ['8 - Jasmin Deen', '8 - San Jose, CL', '8 - 000000122', + '255.00'], ['','','','___________',], ['','','Total:','$3,450.00',], ]; my $cell_props; my $j = 0; foreach my $row ( @{$some_data} ) { my $k = 0; foreach my $cell ( @{$row} ) { if ( ($cell eq 'Name') || ( $cell eq 'Address') || ( $cell eq +'Account') || ( $cell eq 'Amount') ) { $cell_props->[$j]->[$k] ={ background_color => '#808080', # font_color => '#FF0000', # justify => "center", }; }elsif ( ($cell eq 'Primary Info') || ($cell eq 'Secondary Inf +o') ){ $cell_props->[$j]->[$k] = { font => $pdf->corefont("Helvetica" +, -encoding => "utf8"), font_size => 15, background_color => '#FFCA00', # font_color => '#4D4D4D', # justify => "left", }; }elsif ( ($cell eq 'Total:') ){ $cell_props->[$j]->[$k] = { background_color => '#FFCA00', # font_color => '#4D4D4D', # justify => "center", }; }else { $cell_props->[$j]->[$k] = { font => $pdf->corefont("Helvetica" +, -encoding => "utf8"), font_size => 8, background_color => '#ffffff', # font_color => '#000000', # justify => "center", }; } $k++; } $j++; } # build the table layout $pdftable->table( # required params $pdf, $page, $some_data, # 2D arrayref of text strings x => 50, #left_edge_of_table, -w => 495, # width of table. technically optional, but almost + always a good idea to use start_y => 820, # baseline_of_first_line_on_first_page, next_y => 800, # baseline_of_first_line_on_succeeding_pages, -start_h => 800, # height_on_first_page, next_h => 500, # height_on_succeeding_pages, # some optional params -padding => 3, # cell padding padding_right => 10, #right cell padding, overides -pad border => 0, # border width 0 for no border background_color_odd => '', background_color_even => '', #cell background color for even +rows header_props => { min_w => 250, bg_color => "#808080", font => $pdf->corefont("Helvetica", -encoding => "utf8"), font_size => 13, font_color => "#000000", repeat => 1, }, column_props => [ map{{justify => 'center' }}1..4, ], cell_props => $cell_props, ); $pdf->saveas();

Thanks again!

Comment on Colspan in PDF Help!
Download Code
Re: Colspan in PDF Help!
by snoopy (Deacon) on Nov 27, 2012 at 04:28 UTC
    >> I am also trying to make the totals "line" to stretch (colspan) over the table to create a line to divide the sections on this table

    Here's a patch that shows one way of making a solid Total line. It's a bit of cop-out because, rather than attempting to span columns, it's highlighting all cells on rows that contain "Total:".

    @@ -47,6 +47,7 @@ foreach my $row ( @{$some_data} ) { my $k = 0; +my $is_total_line = grep {$_ eq 'Total:'} @$row; foreach my $cell ( @{$row} ) { @@ -67,7 +68,7 @@ font_color => '#4D4D4D', # justify => "left", }; - }elsif ( ($cell eq 'Total:') ){ + }elsif ( $is_total_line ){ $cell_props->[$j]->[$k] = { background_color => '#FFCA00', #
      That's one more good trick I learned, but the issue is if you have table border set to 1 the trick doesn't work because the borders cut the long colored row in cell piece. But thanks for trying!
Re: Colspan in PDF Help!
by igelkott (Curate) on Feb 12, 2013 at 20:59 UTC

    Until you find a proper solution, maybe a crude hack will do.

    Change

    ['Primary Info','','','',],

    to

    ['Primary Info','col$span','col$span','col$span',],

    and similarly for the other lines you wish to "fix". (Obviously, you could use any other string in the place of 'col$span', as long as it won't appear naturally.)

    Then make a new rule for 'col$span' to make the text the same color as the background:

    } elsif ($cell eq 'col$span') { $cell_props->[$j]->[$k] = { font => $pdf->corefont("Helvetica", -encoding => "utf8"), font_size => 1, background_color => '#FFCA00', # font_color => '#FFCA00', # }; }

    Not sure if the Total is exactly what you wanted but the others seem like what you want, even if it is cheating a bit.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005755]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-12-27 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls