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

ted.byers has asked for the wisdom of the Perl Monks concerning the following question:

Here is my column properties specification:

my $col_props1 = [ {min_w => 170,font => $pdf->corefont("Helvetica", -encodin +g => "utf8"),font_size => 10}, {font => $pdf->corefont("Helvetica", -encoding => "utf8"), +font_size => 10,width => 80,justify => 'right'} ];

I am using Acivestate perl 5.16, and the problem is happening with the package PDF::Table.

The objective is to have three or four tables that are about 3.5 inches width, aligned one above the other, but with about an eigth of an inch between each table. That part is easy, but the over all effect looks like garbage because with the above column property specifications, every table ends up with different column widths, as if PDF::Table is ignoring the column specification entirely. I am being asked to make all the tables have the same column widths and nothing I have tried, including specifying minimum and maximum column widths for both columns has produced any consistency at all in the tables produced. (The contents of each table is comprised of a value description in the left column and a dollar amount in the right column, if that matters).

This is driving me insane! What do I have to do to get these tables to have the same column widths? Yes, I have checked the obvious and ensured that all the tables are given the same column properties object (column_props => $col_props1 is the last item in the table constructor).

Thanks.

Ted

  • Comment on PDF::Table seems to ignore column properties when there are but two columns
  • Download Code

Replies are listed 'Best First'.
Re: PDF::Table seems to ignore column properties when there are but two columns
by rjt (Curate) on Jul 09, 2013 at 23:24 UTC

    Works for me:

    #!/usr/bin/env perl use 5.012; use warnings; use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => 'test.pdf'); my $page = $pdf->page; # Table data, two columns my $scalar_song = [ [ '$ Sigils to the left of me', 'Arrows to the right ->' ], [ '$self->here', 'Stuck in the \[] with you' ] ]; my $col_props = [ { font => $pdf->corefont("Courier-Bold"), width => 250, font_color => 'blue', },{ font => $pdf->corefont("Courier-Bold"), width => 250, justify => 'right', font_color => 'green', } ]; $pdftable->table($pdf, $page, $scalar_song, -column_props => $col_props, -start_y => 750, -start_h => 300, -padding => 10, -x => 50, ); $pdf->saveas();

    This generated a .pdf that had two columns of equal width, with the expected font, color, and justification settings. From what I can tell, PDF::Table seems to like leading hyphens on its top-level call, but not the column_props, but the examples in the documentation don't reflect that. Perhaps that is the issue?

      Thanks, but alas, that didn't work for me when I tried to make the columns a different width. On my system, to make your code run, I had to provide a width to the table, in your call to table's constructor. That aside, the colours and aliognment worked fine, regardless of the presence or absence of the leading hyphens. And while it seems easy to make the columns equal width, there seems to be no way to make the right column consistentlly to take one fifth of the table width and the left column to take the rest.

      But to get a better test, try making the values in the left column arbitrary strings of variable length, and the right columns random currency values from $0.00 up to $999,999.99, and have three or four tables that use the same column properties, similar variability in the length of the strings in the left column but order of magnitude differences in the currency values between the tables (i.e. if in the first table the values are of the order of $10,000, make those in the second table of the order of $10, and those in the third of the order of $100,000). What you will see is that the layout of each table adapts to the data it is presenting, instead of the specs in the column properties. It is late, but I will work up an example of this sort in the morning, unless someone beats me to it.

      Thanks again

      Ted

        As promised, here is a sample that shows exactly what I am seeing:

        use strict; use warnings; use PDF::API2; use PDF::Table; my $pdf = new PDF::API2(-file => 'test5.pdf'); my $page = $pdf->page; # Table data, two columns my $table_data1 = [ [ 'balance forward', '$ 0.00' ], [ 'credit', '$ 100.00' ], [ 'debit', '$ 200.00'], [ 'balance', '$ 100.00' ], ]; my $table_data2 = [ [ 'balance forward', '$ 0.00' ], [ 'credit', '$ 10.00' ], [ 'debit', '$ 20.00'], [ 'balance', '$ 10.00' ], ]; my $table_data3 = [ [ 'balance forward', '$ 0.00' ], [ 'credit', '$ 100,000.00' ], [ 'debit', '$ 200,000.00'], [ 'balance', '$ 100,000.00' ], ]; my $table_data4 = [ [ 'balance forward', '$ 0.00' ], [ 'credit', '$ 1.00' ], [ 'debit', '$ 2.00'], [ 'balance balance balance balance', '$ 1.00' ], ]; my $col_props = [ { font => $pdf->corefont("Courier-Bold"), width => 250, font_color => 'blue', },{ font => $pdf->corefont("Courier-Bold"), width => 50, justify => 'right', font_color => 'green', } ]; my $pdftable1 = new PDF::Table; my ($end_page, $pages_spanned, $table_bot_y) = $pdftable1->table($pdf, + $page, $table_data1, -column_props => $col_props, -w => 300, -start_y => 750, -start_h => 300, -padding => 10, -x => 50, ); $table_bot_y -= 5; my $pdftable2 = new PDF::Table; ($end_page, $pages_spanned, $table_bot_y) = $pdftable1->table($pdf, $p +age, $table_data2, -column_props => $col_props, -w => 300, -start_y => $table_bot_y, -start_h => 300, -padding => 10, -x => 50, ); $table_bot_y -= 5; my $pdftable3 = new PDF::Table; ($end_page, $pages_spanned, $table_bot_y) = $pdftable1->table($pdf, $p +age, $table_data3, -column_props => $col_props, -w => 300, -start_y => $table_bot_y, -start_h => 300, -padding => 10, -x => 50, ); my $pdftable4 = new PDF::Table; $table_bot_y -= 5; $pdftable1->table($pdf, $page, $table_data4, -column_props => $col_props, -w => 300, -start_y => $table_bot_y, -start_h => 300, -padding => 5, -x => 50, ); $pdf->saveas();

        What you will see is that the third table gets a right column that is bigger than the rest, and then the fourth gets a right column that is narrower than the rest. What is required is that the table actually respect the properties specified in col_props, so that I can adjust the width of the right column so that it will always accomodate the largest currency value I know exists in my data, and use that in all tables using a given col_props variable.

        But I did notice that if I use a single instance of 'balance' in the fourth table, the table uses what looks like the same widths as those in the third table. Does the function 'table' actually modify col_props in those cases when it has the temerity to over-ride the values I coded? If so, what can be done about that?

        Thanks

        Ted