http://www.perlmonks.org?node_id=1004034

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

Hi Monks!
I am trying to align a text inside of this pdf table cells and having no success. I can do the cell background color but to adjust the font size doesn't work at all. I am trying to set the font size for the sub-heading texts like, "Primary Info" and "Secondary Info" and I just cant figure this one out, I wish the module would give an easier access to these settings in an easier way.
Has anyone worked with this before that could show me how or a better way of doing this? I am also trying to insert a line where the sum results are using a graphic or something other than faking the sub totals line.
Any Help is very appreciated. Here is the code sample to illustrade my frustration with this:

#!/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 for looking!

Replies are listed 'Best First'.
Re: Set text size in table cell using PDF::Table
by Eliya (Vicar) on Nov 15, 2012 at 17:30 UTC

    Works for me with the current version of PDF::Table (0.9.6)  -  but not with an older version (0.9.3).

    Also, note that $pdf->corefont("Helvetica", -encoding => "utf8") creates and embeds a font instance every time you make that call.  I.e., an optmization would be to create the font object just once

    my $helvetica = $pdf->corefont("Helvetica", -encoding => "utf8");

    and then just pass the same reference to the individual cells

    ... $cell_props->[$j]->[$k] = { font => $helvetica, font_size => 15, background_color => '#FFCA00', # font_color => '#fD4D4D', # justify => "left", }; ...

    This cuts down the PDF size for the sample code from 290k to 9k.

      Thank you that worked, I updated to the newer version.I just need to figured it out how to add the line thing for the subtotals, thanks!
      Hi there, I am doing something similar and have a question. Is it possible using the same similar code here to do "colspan", for a sub header for example. Thanks!