7 use List::Util qw(sum);
8 our $VERSION = '0.9.10';
10 print __PACKAGE__.' is version: '.$VERSION.$/ if($ENV{'PDF_TABLE_DEBUG'});
12 ############################################################
16 # Parameters are meta information about the PDF
18 # $pdf = PDF::Table->new();
20 ############################################################
25 my $class = ref($type) || $type;
27 bless ($self, $class);
29 # Pass all the rest to init for validation and initialisation
37 my ($self, $pdf, $page, $data, %options ) = @_;
39 # Check and set default values
40 $self->set_defaults();
42 # Check and set mandatory params
44 $self->set_page($page);
45 $self->set_data($data);
46 $self->set_options(\%options);
54 $self->{'font_size'} = 12;
58 my ($self, $pdf) = @_;
59 $self->{'pdf'} = $pdf;
63 my ($self, $page) = @_;
64 if ( defined($page) && ref($page) ne 'PDF::API2::Page' ){
66 if( ref($self->{'pdf'}) eq 'PDF::API2' ){
67 $self->{'page'} = $self->{'pdf'}->page();
69 carp 'Warning: Page must be a PDF::API2::Page object but it seems to be: '.ref($page).$/;
70 carp 'Error: Cannot set page from passed PDF object either as it is invalid!'.$/;
74 $self->{'page'} = $page;
79 my ($self, $data) = @_;
84 my ($self, $options) = @_;
88 ############################################################
90 # text_block - utility method to build multi-paragraph blocks of text
92 ############################################################
97 my $text_object = shift;
98 my $text = shift; # The text to be displayed
99 my %arg = @_; # Additional Arguments
101 my ( $align, $xpos, $ypos, $xbase, $ybase, $line_width, $wordspace, $endw , $width, $height) =
102 ( undef , undef, undef, undef , undef , undef , undef , undef , undef , undef );
103 my @line = (); # Temp data array with words on one line
104 my %width = (); # The width of every unique word in the givven text
106 # Try to provide backward compatibility
107 foreach my $key (keys %arg)
110 if($newkey =~ s#^-##)
112 $arg{$newkey} = $arg{$key};
119 # Lets check mandatory parameters with no default values
121 $xbase = $arg{'x'} || -1;
122 $ybase = $arg{'y'} || -1;
123 $width = $arg{'w'} || -1;
124 $height = $arg{'h'} || -1;
125 unless( $xbase > 0 ){ carp "Error: Left Edge of Block is NOT defined!\n"; return; }
126 unless( $ybase > 0 ){ carp "Error: Base Line of Block is NOT defined!\n"; return; }
127 unless( $width > 0 ){ carp "Error: Width of Block is NOT defined!\n"; return; }
128 unless( $height > 0 ){ carp "Error: Height of Block is NOT defined!\n"; return; }
129 # Check if any text to display
130 unless( defined( $text) and length($text) > 0 )
132 carp "Warning: No input text found. Trying to add dummy '-' and not to break everything.\n";
136 # Strip any <CR> and Split the text into paragraphs
138 my @paragraphs = split(/\n/, $text);
140 # Width between lines in pixels
141 my $line_space = defined $arg{'lead'} && $arg{'lead'} > 0 ? $arg{'lead'} : 12;
143 # Calculate width of all words
144 my $space_width = $text_object->advancewidth("\x20");
145 my @words = split(/\s+/, $text);
148 next if exists $width{$_};
149 $width{$_} = $text_object->advancewidth($_);
152 my @paragraph = split(' ', shift(@paragraphs));
154 my $first_paragraph = 1;
159 $ypos = $ybase + $line_space;
160 my $bottom_border = $ypos - $height;
161 # While we can add another line
162 while ( $ypos >= $bottom_border + $line_space )
164 # Is there any text to render ?
167 # Finish if nothing left
168 last unless scalar @paragraphs;
169 # Else take one line from the text
170 @paragraph = split(' ', shift( @paragraphs ) );
172 $ypos -= $arg{'parspace'} if $arg{'parspace'};
173 last unless $ypos >= $bottom_border;
175 $ypos -= $line_space;
178 # While there's room on the line, add another word
181 if( $first_line && exists $arg{'hang'} )
183 my $hang_width = $text_object->advancewidth($arg{'hang'});
185 $text_object->translate( $xpos, $ypos );
186 $text_object->text( $arg{'hang'} );
188 $xpos += $hang_width;
189 $line_width += $hang_width;
190 $arg{'indent'} += $hang_width if $first_paragraph;
192 elsif( $first_line && exists $arg{'flindent'} && $arg{'flindent'} > 0 )
194 $xpos += $arg{'flindent'};
195 $line_width += $arg{'flindent'};
197 elsif( $first_paragraph && exists $arg{'fpindent'} && $arg{'fpindent'} > 0 )
199 $xpos += $arg{'fpindent'};
200 $line_width += $arg{'fpindent'};
202 elsif (exists $arg{'indent'} && $arg{'indent'} > 0 )
204 $xpos += $arg{'indent'};
205 $line_width += $arg{'indent'};
208 # Lets take from paragraph as many words as we can put into $width - $indent;
209 while ( @paragraph and $text_object->advancewidth( join("\x20", @line)."\x20" . $paragraph[0]) +
210 $line_width < $width )
212 push(@line, shift(@paragraph));
214 $line_width += $text_object->advancewidth(join('', @line));
216 # calculate the space width
217 if( $arg{'align'} eq 'fulljustify' or ($arg{'align'} eq 'justify' and @paragraph))
219 @line = split(//,$line[0]) if (scalar(@line) == 1) ;
220 $wordspace = ($width - $line_width) / (scalar(@line) - 1);
225 $align=($arg{'align'} eq 'justify') ? 'left' : $arg{'align'};
226 $wordspace = $space_width;
228 $line_width += $wordspace * (scalar(@line) - 1);
230 if( $align eq 'justify')
232 foreach my $word (@line)
234 $text_object->translate( $xpos, $ypos );
235 $text_object->text( $word );
236 $xpos += ($width{$word} + $wordspace) if (@line);
242 # calculate the left hand position of the line
243 if( $align eq 'right' )
245 $xpos += $width - $line_width;
247 elsif( $align eq 'center' )
249 $xpos += ( $width / 2 ) - ( $line_width / 2 );
253 $text_object->translate( $xpos, $ypos );
254 $endw = $text_object->text( join("\x20", @line));
258 unshift(@paragraphs, join(' ',@paragraph)) if scalar(@paragraph);
259 return ($endw, $ypos, join("\n", @paragraphs))
263 ################################################################
264 # table - utility method to build multi-row, multicolumn tables
265 ################################################################
274 #=====================================
275 # Mandatory Arguments Section
276 #=====================================
277 unless($pdf and $page and $data)
279 carp "Error: Mandatory parameter is missing pdf/page/data object!\n";
283 # Validate mandatory argument data type
284 croak "Error: Invalid pdf object received." unless (ref($pdf) eq 'PDF::API2');
285 croak "Error: Invalid page object received." unless (ref($page) eq 'PDF::API2::Page');
286 croak "Error: Invalid data received." unless ((ref($data) eq 'ARRAY') && scalar(@$data));
287 croak "Error: Missing required settings." unless (scalar(keys %arg));
289 # Validate settings key
290 my %valid_settings_key = (
303 background_color => 1,
304 background_color_odd => 1,
305 background_color_even => 1,
308 horizontal_borders => 1,
309 vertical_borders => 1,
313 font_color_even => 1,
314 background_color_odd => 1,
315 background_color_even => 1,
321 max_word_length => 1,
323 foreach my $key (keys %arg) {
324 croak "Error: Invalid setting key '$key' received."
325 unless (exists $valid_settings_key{$key});
328 # Try to provide backward compatibility
329 foreach my $key (keys %arg)
332 if($newkey =~ s#^-##)
334 $arg{$newkey} = $arg{$key};
340 #TODO: Add code for header props compatibility and col_props comp....
342 my ( $xbase, $ybase, $width, $height ) = ( undef, undef, undef, undef );
343 # Could be 'int' or 'real' values
344 $xbase = $arg{'x' } || -1;
345 $ybase = $arg{'start_y'} || -1;
346 $width = $arg{'w' } || -1;
347 $height = $arg{'start_h'} || -1;
349 # Global geometry parameters are also mandatory.
350 unless( $xbase > 0 ){ carp "Error: Left Edge of Table is NOT defined!\n"; return; }
351 unless( $ybase > 0 ){ carp "Error: Base Line of Table is NOT defined!\n"; return; }
352 unless( $width > 0 ){ carp "Error: Width of Table is NOT defined!\n"; return; }
353 unless( $height > 0 ){ carp "Error: Height of Table is NOT defined!\n"; return; }
355 # Ensure default values for -next_y and -next_h
356 my $next_y = $arg{'next_y'} || $arg{'start_y'} || 0;
357 my $next_h = $arg{'next_h'} || $arg{'start_h'} || 0;
360 my $txt = $page->text;
362 # Set Default Properties
363 my $fnt_name = $arg{'font' } || $pdf->corefont('Times',-encode => 'utf8');
364 my $fnt_size = $arg{'font_size' } || 12;
365 my $max_word_len= $arg{'max_word_length' } || 20;
367 #=====================================
368 # Table Header Section
369 #=====================================
370 # Disable header row into the table
371 my $header_props = undef;
373 # Check if the user enabled it ?
374 if(defined $arg{'header_props'} and ref( $arg{'header_props'}) eq 'HASH')
376 # Transfer the reference to local variable
377 $header_props = $arg{'header_props'};
379 # Check other params and put defaults if needed
380 $header_props->{'repeat' } = $header_props->{'repeat' } || 0;
381 $header_props->{'font' } = $header_props->{'font' } || $fnt_name;
382 $header_props->{'font_color' } = $header_props->{'font_color' } || '#000066';
383 $header_props->{'font_size' } = $header_props->{'font_size' } || $fnt_size + 2;
384 $header_props->{'bg_color' } = $header_props->{'bg_color' } || '#FFFFAA';
385 $header_props->{'justify' } = $header_props->{'justify' };
388 my $header_row = undef;
389 #=====================================
390 # Other Parameters check
391 #=====================================
392 my $lead = $arg{'lead' } || $fnt_size;
393 my $pad_left = $arg{'padding_left' } || $arg{'padding'} || 0;
394 my $pad_right = $arg{'padding_right' } || $arg{'padding'} || 0;
395 my $pad_top = $arg{'padding_top' } || $arg{'padding'} || 0;
396 my $pad_bot = $arg{'padding_bottom'} || $arg{'padding'} || 0;
397 my $line_w = defined $arg{'border'} ? $arg{'border'} : 1 ;
398 my $horiz_borders = defined $arg{'horizontal_borders'}
399 ? $arg{'horizontal_borders'}
401 my $vert_borders = defined $arg{'vertical_borders'}
402 ? $arg{'vertical_borders'}
405 my $background_color_even = $arg{'background_color_even' } || $arg{'background_color'} || undef;
406 my $background_color_odd = $arg{'background_color_odd' } || $arg{'background_color'} || undef;
407 my $font_color_even = $arg{'font_color_even' } || $arg{'font_color' } || 'black';
408 my $font_color_odd = $arg{'font_color_odd' } || $arg{'font_color' } || 'black';
409 my $border_color = $arg{'border_color' } || 'black';
411 my $min_row_h = $fnt_size + $pad_top + $pad_bot;
412 my $row_h = defined ($arg{'row_height'})
414 ($arg{'row_height'} > $min_row_h)
416 $arg{'row_height'} : $min_row_h;
420 my $cell_props = $arg{cell_props} || []; # per cell properties
422 #If there is no valid data array reference warn and return!
423 if(ref $data ne 'ARRAY')
425 carp "Passed table data is not an ARRAY reference. It's actually a ref to ".ref($data);
426 return ($page,0,$cur_y);
429 # Copy the header row if header is enabled
430 @$header_row = $$data[0] if defined $header_props;
431 # Determine column widths based on content
433 # an arrayref whose values are a hashref holding
434 # the minimum and maximum width of that column
435 my $col_props = $arg{'column_props'} || [];
437 # An array ref of arrayrefs whose values are
438 # the actual widths of the column/row intersection
439 my $row_col_widths = [];
440 # An array ref with the widths of the header row
441 my $header_row_props = [];
443 # Scalars that hold sum of the maximum and minimum widths of all columns
444 my ( $max_col_w , $min_col_w ) = ( 0,0 );
445 my ( $row, $col_name, $col_fnt_size, $space_w );
447 my $word_widths = {};
448 my $rows_height = [];
451 for( my $row_idx = 0; $row_idx < scalar(@$data) ; $row_idx++ )
453 my $column_widths = []; #holds the width of each column
454 # Init the height for this row
455 $rows_height->[$row_idx] = 0;
457 for( my $column_idx = 0; $column_idx < scalar(@{$data->[$row_idx]}) ; $column_idx++ )
459 # look for font information for this column
460 my ($cell_font, $cell_font_size);
462 if( !$row_idx and ref $header_props )
464 $cell_font = $header_props->{'font'};
465 $cell_font_size = $header_props->{'font_size'};
468 # Get the most specific value if none was already set from header_props
469 $cell_font ||= $cell_props->[$row_idx][$column_idx]->{'font'}
470 || $col_props->[$column_idx]->{'font'}
473 $cell_font_size ||= $cell_props->[$row_idx][$column_idx]->{'font_size'}
474 || $col_props->[$column_idx]->{'font_size'}
478 $txt->font( $cell_font, $cell_font_size );
480 # Set row height to biggest font size from row's cells
481 if( $cell_font_size > $rows_height->[$row_idx] )
483 $rows_height->[$row_idx] = $cell_font_size;
486 # This should fix a bug with very long words like serial numbers etc.
487 if( $max_word_len > 0 )
489 $data->[$row_idx][$column_idx] =~ s#(\S{$max_word_len})(?=\S)#$1 #g;
492 # Init cell size limits
493 $space_w = $txt->advancewidth( "\x20" );
494 $column_widths->[$column_idx] = 0;
498 my @words = split( /\s+/, $data->[$row_idx][$column_idx] );
502 unless( exists $word_widths->{$_} )
503 { # Calculate the width of every word and add the space width to it
504 $word_widths->{$_} = $txt->advancewidth( $_ ) + $space_w;
507 $column_widths->[$column_idx] += $word_widths->{$_};
508 $min_col_w = $word_widths->{$_} if( $word_widths->{$_} > $min_col_w );
509 $max_col_w += $word_widths->{$_};
512 $min_col_w += $pad_left + $pad_right;
513 $max_col_w += $pad_left + $pad_right;
514 $column_widths->[$column_idx] += $pad_left + $pad_right;
516 # Keep a running total of the overall min and max widths
517 $col_props->[$column_idx]->{'min_w'} ||= 0;
518 $col_props->[$column_idx]->{'max_w'} ||= 0;
520 if( $min_col_w > $col_props->[$column_idx]->{'min_w'} )
521 { # Calculated Minimum Column Width is more than user-defined
522 $col_props->[$column_idx]->{'min_w'} = $min_col_w ;
525 if( $max_col_w > $col_props->[$column_idx]->{'max_w'} )
526 { # Calculated Maximum Column Width is more than user-defined
527 $col_props->[$column_idx]->{'max_w'} = $max_col_w ;
529 }#End of for(my $column_idx....
531 $row_col_widths->[$row_idx] = $column_widths;
533 # Copy the calculated row properties of header row.
534 @$header_row_props = @$column_widths if(!$row_idx and ref $header_props);
537 # Calc real column widths and expand table width if needed.
538 my $calc_column_widths;
539 ($calc_column_widths, $width) = CalcColumnWidths( $col_props, $width );
540 my $num_cols = scalar @{ $calc_column_widths };
542 # Lets draw what we have!
544 # Store header row height for later use if headers have to be repeated
545 my $header_row_height = $rows_height->[0];
547 my ( $gfx, $gfx_bg, $background_color, $font_color, $bot_marg, $table_top_y, $text_start);
549 # Each iteration adds a new page as neccessary
550 while(scalar(@{$data}))
552 my ($page_header, $columns_number);
556 $table_top_y = $ybase;
557 $bot_marg = $table_top_y - $height;
561 if(ref $arg{'new_page_func'})
563 $page = &{$arg{'new_page_func'}};
570 $table_top_y = $next_y;
571 $bot_marg = $table_top_y - $next_h;
573 if( ref $header_props and $header_props->{'repeat'})
576 @$page_header = @$header_row;
578 @$hrp = @$header_row_props ;
579 # Then prepend it to master data array
580 unshift @$data, @$page_header;
581 unshift @$row_col_widths, $hrp;
582 unshift @$rows_height, $header_row_height;
584 $first_row = 1; # Means YES
585 $row_index--; # Rollback the row_index because a new header row has been added
589 # Check for safety reasons
591 { # This warning should remain i think
592 # carp "!!! Warning: !!! Incorrect Table Geometry! Setting bottom margin to end of sheet!\n";
596 $gfx_bg = $page->gfx;
598 $txt->font($fnt_name, $fnt_size);
600 $cur_y = $table_top_y;
605 $gfx->strokecolor($border_color);
606 $gfx->linewidth($line_w);
611 $gfx->move( $xbase , $cur_y );
612 $gfx->hline($xbase + $width );
620 # Each iteration adds a row to the current page until the page is full
621 # or there are no more rows to add
623 while(scalar(@{$data}) and $cur_y-$row_h > $bot_marg)
625 # Remove the next item from $data
626 my $record = shift @{$data};
628 # Get columns number to know later how many vertical lines to draw
629 # TODO: get the max number of columns per page as currently last row's columns overrides
630 $columns_number = scalar(@$record);
632 # Get the next set of row related settings
634 my $pre_calculated_row_height = shift @$rows_height;
637 my $record_widths = shift @$row_col_widths;
639 # Row coloumn props - TODO in another commit
641 # Row cell props - TODO in another commit
643 # Added to resolve infite loop bug with returned undef values
644 for(my $d = 0; $d < scalar(@{$record}) ; $d++)
646 $record->[$d] = '-' unless( defined $record->[$d]);
649 # Choose colors for this row
650 $background_color = $row_index % 2 ? $background_color_even : $background_color_odd;
651 $font_color = $row_index % 2 ? $font_color_even : $font_color_odd;
653 #Determine current row height
654 my $current_row_height = $pad_top + $pre_calculated_row_height + $pad_bot;
656 # $row_h is the calculated global user requested row height.
657 # It will be honored, only if it has bigger value than the calculated one.
658 # TODO: It's questionable if padding should be inclided in this calculation or not
659 if($current_row_height < $row_h){
660 $current_row_height = $row_h;
663 # Define the font y base position for this line.
664 $text_start = $cur_y - ($current_row_height - $pad_bot);
667 my $leftovers = undef; # Reference to text that is returned from textblock()
668 my $do_leftovers = 0;
669 my ($colspan, @vertical_lines);
671 # Process every cell(column) from current row
672 for( my $column_idx = 0; $column_idx < scalar( @$record); $column_idx++ )
674 next unless $col_props->[$column_idx]->{'max_w'};
675 next unless $col_props->[$column_idx]->{'min_w'};
676 $leftovers->[$column_idx] = undef;
678 # look for font information for this cell
679 my ($cell_font, $cell_font_size, $cell_font_color, $justify);
681 if( $first_row and ref $header_props)
683 $cell_font = $header_props->{'font'};
684 $cell_font_size = $header_props->{'font_size'};
685 $cell_font_color = $header_props->{'font_color'};
686 $justify = $header_props->{'justify'};
689 # Get the most specific value if none was already set from header_props
690 $cell_font ||= $cell_props->[$row_index][$column_idx]->{'font'}
691 || $col_props->[$column_idx]->{'font'}
694 $cell_font_size ||= $cell_props->[$row_index][$column_idx]->{'font_size'}
695 || $col_props->[$column_idx]->{'font_size'}
698 $cell_font_color ||= $cell_props->[$row_index][$column_idx]->{'font_color'}
699 || $col_props->[$column_idx]->{'font_color'}
702 $justify ||= $cell_props->[$row_index][$column_idx]->{'justify'}
703 || $col_props->[$column_idx]->{'justify'}
707 # Init cell font object
708 $txt->font( $cell_font, $cell_font_size );
709 $txt->fillcolor($cell_font_color);
712 if (!$first_row && $cell_props->[$row_index][$column_idx]->{colspan}) {
713 $colspan = -1 == $cell_props->[$row_index][$column_idx]->{colspan}
714 ? $num_cols - $column_idx
715 : $cell_props->[$row_index][$column_idx]->{colspan};
716 my $last_idx = $column_idx + $colspan - 1;
717 $this_width = sum @{ $calc_column_widths }[$column_idx..$last_idx];
720 $this_width = $calc_column_widths->[$column_idx];
723 # If the content is wider than the specified width, we need to add the text as a text block
724 if( $record->[$column_idx] !~ m/(.\n.)/ and
725 $record_widths->[$column_idx] and
726 $record_widths->[$column_idx] <= $this_width
728 my $space = $pad_left;
729 if ($justify eq 'right')
731 $space = $this_width -($txt->advancewidth($record->[$column_idx]) + $pad_right);
733 elsif ($justify eq 'center')
735 $space = ($this_width - $txt->advancewidth($record->[$column_idx])) / 2;
737 $txt->translate( $cur_x + $space, $text_start );
738 $txt->text( $record->[$column_idx] );
740 # Otherwise just use the $page->text() method
743 my ($width_of_last_line, $ypos_of_last_line, $left_over_text) = $self->text_block(
745 $record->[$column_idx],
746 x => $cur_x + $pad_left,
748 w => $this_width - $pad_left - $pad_right,
749 h => $cur_y - $bot_marg - $pad_top - $pad_bot,
753 # Desi - Removed $lead because of fixed incorrect ypos bug in text_block
754 my $current_cell_height = $cur_y - $ypos_of_last_line + $pad_bot;
755 if( $current_cell_height > $current_row_height )
757 $current_row_height = $current_cell_height;
760 if( $left_over_text )
762 $leftovers->[$column_idx] = $left_over_text;
766 $cur_x += $calc_column_widths->[$column_idx];
768 push @vertical_lines, (!$colspan || (1 >= $colspan)) ? 1 : 0;
769 $colspan-- if $colspan;
773 unshift @$data, $leftovers;
774 unshift @$row_col_widths, $record_widths;
775 unshift @$rows_height, $pre_calculated_row_height;
779 # This has to be separately from the text loop
780 # because we do not know the final height of the cell until all text has been drawn
782 for(my $column_idx = 0 ; $column_idx < scalar(@$record) ; $column_idx++)
786 if( $first_row and ref $header_props)
787 { #Compatibility Consistency with other props
788 $cell_bg_color = $header_props->{'bg_color'} || $header_props->{'background_color'};
791 # Get the most specific value if none was already set from header_props
792 $cell_bg_color ||= $cell_props->[$row_index][$column_idx]->{'background_color'}
793 || $col_props->[$column_idx]->{'background_color'}
794 || $background_color;
798 $gfx_bg->rect( $cur_x, $cur_y-$current_row_height, $calc_column_widths->[$column_idx], $current_row_height);
799 $gfx_bg->fillcolor($cell_bg_color);
802 $cur_x += $calc_column_widths->[$column_idx];
804 if ($line_w && $vertical_lines[$column_idx] && ($column_idx != (scalar(@{ $record }) - 1))) {
805 $gfx->move($cur_x, $cur_y);
806 $gfx->vline($cur_y - $row_h);
807 $gfx->fillcolor($border_color);
809 }#End of for(my $column_idx....
811 $cur_y -= $current_row_height;
812 if ($gfx && $horiz_borders)
814 $gfx->move( $xbase , $cur_y );
815 $gfx->hline( $xbase + $width );
818 $row_index++ unless ( $do_leftovers );
824 # Draw vertical lines
827 $gfx->move( $xbase, $table_top_y);
828 $gfx->vline( $cur_y );
829 $gfx->move($xbase + sum(@{ $calc_column_widths }[0..$num_cols - 1]), $table_top_y);
830 $gfx->vline( $cur_y );
833 # ACTUALLY draw all the lines
834 $gfx->fillcolor( $border_color);
838 }# End of while(scalar(@{$data}))
840 return ($page,--$pg_cnt,$cur_y);
844 # calculate the column widths
847 my $col_props = shift;
848 my $avail_width = shift;
853 for(my $j = 0; $j < scalar( @$col_props); $j++)
855 $min_width += $col_props->[$j]->{min_w} || 0;
858 # I think this is the optimal variant when good view can be guaranateed
859 if($avail_width < $min_width)
861 carp "!!! Warning !!!\n Calculated Mininal width($min_width) > Table width($avail_width).\n",
862 ' Expanding table width to:',int($min_width)+1,' but this could lead to unexpected results.',"\n",
863 ' Possible solutions:',"\n",
864 ' 0)Increase table width.',"\n",
865 ' 1)Decrease font size.',"\n",
866 ' 2)Choose a more narrow font.',"\n",
867 ' 3)Decrease "max_word_length" parameter.',"\n",
868 ' 4)Rotate page to landscape(if it is portrait).',"\n",
869 ' 5)Use larger paper size.',"\n",
870 '!!! --------- !!!',"\n";
871 $avail_width = int( $min_width) + 1;
875 # Calculate how much can be added to every column to fit the available width.
876 for(my $j = 0; $j < scalar(@$col_props); $j++ )
878 $calc_widths->[$j] = $col_props->[$j]->{min_w} || 0;;
881 # Allow columns to expand to max_w before applying extra space equally.
885 my $span = ($avail_width - $min_width) / scalar( @$col_props);
889 my $next_will_be_last_iter = 1;
890 for(my $j = 0; $j < scalar(@$col_props); $j++ )
892 my $new_w = $calc_widths->[$j] + $span;
894 if (!$is_last_iter && $new_w > $col_props->[$j]->{max_w})
896 $new_w = $col_props->[$j]->{max_w}
898 if ($calc_widths->[$j] != $new_w )
900 $calc_widths->[$j] = $new_w;
901 $next_will_be_last_iter = 0;
903 $min_width += $new_w;
905 last if $is_last_iter;
906 $is_last_iter = $next_will_be_last_iter;
909 return ($calc_widths,$avail_width);
919 PDF::Table - A utility class for building table layouts in a PDF::API2 object.
926 my $pdftable = new PDF::Table;
927 my $pdf = new PDF::API2(-file => "table_of_lorem.pdf");
928 my $page = $pdf->page;
930 # some data to layout
932 ["1 Lorem ipsum dolor",
933 "Donec odio neque, faucibus vel",
934 "consequat quis, tincidunt vel, felis."],
935 ["Nulla euismod sem eget neque.",
941 $left_edge_of_table = 50;
942 # build the table layout
948 x => $left_edge_of_table,
952 # some optional params
957 background_color_odd => "gray",
958 background_color_even => "lightblue", #cell background color for even rows
961 # do other stuff with $pdf
967 For a complete working example or initial script look into distribution`s 'examples' folder.
972 This class is a utility for use with the PDF::API2 module from CPAN.
973 It can be used to display text data in a table layout within a PDF.
974 The text data must be in a 2D array (such as returned by a DBI statement handle fetchall_arrayref() call).
975 The PDF::Table will automatically add as many new pages as necessary to display all of the data.
976 Various layout properties, such as font, font size, and cell padding and background color can be specified for each column and/or for even/odd rows.
977 Also a (non)repeated header row with different layout properties can be specified.
979 See the L</METHODS> section for complete documentation of every parameter.
985 my $pdf_table = new PDF::Table;
991 Creates a new instance of the class. (to be improved)
995 There are no parameters.
999 Reference to the new instance
1005 my ($final_page, $number_of_pages, $final_y) = table($pdf, $page, $data, %settings)
1011 Generates a multi-row, multi-column table into an existing PDF document based on provided data set and settings.
1015 $pdf - a PDF::API2 instance representing the document being created
1016 $page - a PDF::API2::Page instance representing the current page of the document
1017 $data - an ARRAY reference to a 2D data structure that will be used to build the table
1018 %settings - HASH with geometry and formatting parameters.
1020 For full %settings description see section L</Table settings> below.
1022 This method will add more pages to the pdf instance as required based on the formatting options and the amount of data.
1026 The return value is a 3 items list where
1028 $final_page - The first item is a PDF::API2::Page instance that the table ends on
1029 $number_of_pages - The second item is the count of pages that the table spans on
1030 $final_y - The third item is the Y coordinate of the table bottom so that additional content can be added in the same document.
1034 my $pdf = new PDF::API2;
1035 my $page = $pdf->page();
1037 ['foo1','bar1','baz1'],
1038 ['foo2','bar2','baz2']
1047 my ($final_page, $number_of_pages, $final_y) = $pdftable->table( $pdf, $page, $data, %options );
1051 =head3 Table settings
1055 There are some mandatory parameteres for setting table geometry and position across page(s)
1059 =item B<x> - X coordinate of upper left corner of the table. Left edge of the sheet is 0.
1061 B<Value:> can be any whole number satisfying 0 =< X < PageWidth
1062 B<Default:> No default value
1066 =item B<start_y> - Y coordinate of upper left corner of the table at the initial page.
1068 B<Value:> can be any whole number satisfying 0 < start_y < PageHeight (depending on space availability when embedding a table)
1069 B<Default:> No default value
1073 =item B<w> - width of the table starting from X.
1075 B<Value:> can be any whole number satisfying 0 < w < PageWidth - x
1076 B<Default:> No default value
1080 =item B<start_h> - Height of the table on the initial page
1082 B<Value:> can be any whole number satisfying 0 < start_h < PageHeight - Current Y position
1083 B<Default:> No default value
1093 =item B<next_h> - Height of the table on any additional page
1095 B<Value:> can be any whole number satisfying 0 < next_h < PageHeight
1096 B<Default:> Value of param B<'start_h'>
1100 =item B<next_y> - Y coordinate of upper left corner of the table at any additional page.
1102 B<Value:> can be any whole number satisfying 0 < next_y < PageHeight
1103 B<Default:> Value of param B<'start_y'>
1107 =item B<max_word_length> - Breaks long words (like serial numbers hashes etc.) by adding a space after every Nth symbol
1109 B<Value:> can be any whole positive number
1112 max_word_length => 20 # Will add a space after every 20 symbols
1114 =item B<padding> - Padding applied to every cell
1116 =item B<padding_top> - top cell padding, overrides 'padding'
1118 =item B<padding_right> - right cell padding, overrides 'padding'
1120 =item B<padding_left> - left cell padding, overrides 'padding'
1122 =item B<padding_bottom> - bottom padding, overrides 'padding'
1124 B<Value:> can be any whole positive number
1126 B<Default padding:> 0
1128 B<Default padding_*> $padding
1130 padding => 5 # all sides cell padding
1131 padding_top => 8, # top cell padding, overrides 'padding'
1132 padding_right => 6, # right cell padding, overrides 'padding'
1133 padding_left => 2, # left cell padding, overrides 'padding'
1134 padding_bottom => undef # bottom padding will be 5 as it will fallback to 'padding'
1136 =item B<border> - Width of table border lines.
1138 =item B<horizontal_borders> - Width of horizontal border lines. Overrides 'border' value.
1140 =item B<vertical_borders> - Width of vertical border lines. Overrides 'border' value.
1142 B<Value:> can be any whole positive number. When set to 0 will disable border lines.
1145 border => 3 # border width is 3
1146 horizontal_borders => 1 # horizontal borders will be 1 overriding 3
1147 vertical_borders => undef # vertical borders will be 3 as it will fallback to 'border'
1149 =item B<vertical_borders> - Width of vertical border lines. Overrides 'border' value.
1151 B<Value:> Color specifier as 'name' or 'HEX'
1154 border_color => 'red'
1156 =item B<font> - instance of PDF::API2::Resource::Font defining the fontf to be used in the table
1158 B<Value:> can be any PDF::API2::Resource::* type of font
1159 B<Default:> 'Times' with UTF8 encoding
1161 font => $pdf->corefont("Helvetica", -encoding => "utf8")
1163 =item B<font_size> - Default size of the font that will be used across the table
1165 B<Value:> can be any positive number
1170 =item B<font_color> - Font color for all rows
1172 =item B<font_color_odd> - Font color for odd rows
1174 =item B<font_color_even> - Font color for even rows
1176 =item B<background_color_odd> - Background color for odd rows
1178 =item B<background_color_even> - Background color for even rows
1180 B<Value:> Color specifier as 'name' or 'HEX'
1181 B<Default:> 'black' font on 'white' background
1183 font_color => '#333333'
1184 font_color_odd => 'purple'
1185 font_color_even => '#00FF00'
1186 background_color_odd => 'gray'
1187 background_color_even => 'lightblue'
1189 =item B<row_height> - Desired row height but it will be honored only if row_height > font_size + padding_top + padding_bottom
1191 B<Value:> can be any whole positive number
1192 B<Default:> font_size + padding_top + padding_bottom
1196 =item B<new_page_func> - CODE reference to a function that returns a PDF::API2::Page instance.
1198 If used the parameter 'new_page_func' must be a function reference which when executed will create a new page and will return the object back to the module.
1199 For example you can use it to put Page Title, Page Frame, Page Numbers and other staff that you need.
1200 Also if you need some different type of paper size and orientation than the default A4-Portrait for example B2-Landscape you can use this function ref to set it up for you. For more info about creating pages refer to PDF::API2 PAGE METHODS Section.
1201 Don't forget that your function must return a page object created with PDF::API2 page() method.
1203 new_page_func => $code_ref
1205 =item B<header_props> - HASH reference to specific settings for the Header row of the table. See section L</Header Row Properties> below
1207 header_props => $hdr_props
1209 =item B<column_props> - HASH reference to specific settings for each column of the table. See section L</Column Properties> below
1211 column_props => $col_props
1213 =item B<cell_props> - HASH reference to specific settings for each column of the table. See section L</Cell Properties> below
1215 cell_props => $cel_props
1219 =head4 Header Row Properties
1221 If the 'header_props' parameter is used, it should be a hashref. Passing an empty HASH will trigger a header row initialised with Default values.
1222 There is no 'data' variable for the content, because the module asumes that first table row will become the header row. It will copy this row and put it on every new page if 'repeat' param is set.
1226 =item B<font> - instance of PDF::API2::Resource::Font defining the fontf to be used in the header row
1228 B<Value:> can be any PDF::API2::Resource::* type of font
1229 B<Default:> 'font' of the table. See table parameter 'font' for more details.
1231 =item B<font_size> - Font size of the header row
1233 B<Value:> can be any positive number
1234 B<Default:> 'font_size' of the table + 2
1236 =item B<font_color> - Font color of the header row
1238 B<Value:> Color specifier as 'name' or 'HEX'
1239 B<Default:> '#000066'
1241 =item B<bg_color> - Background color of the header row
1243 B<Value:> Color specifier as 'name' or 'HEX'
1246 =item B<repeat> - Flag showing if header row should be repeated on every new page
1248 B<Value:> 0,1 1-Yes/True, 0-No/False
1251 =item B<justify> - Alignment of text in the header row.
1253 B<Value:> One of 'left', 'right', 'center'
1254 B<Default:> Same as column alignment (or 'left' if undefined)
1258 font => $pdf->corefont("Helvetica", -encoding => "utf8"),
1260 font_color => '#004444',
1261 bg_color => 'yellow',
1268 =head4 Column Properties
1270 If the 'column_props' parameter is used, it should be an arrayref of hashrefs,
1271 with one hashref for each column of the table. The columns are counted from left to right so the hash reference at $col_props[0] will hold properties for the first column from left to right.
1272 If you DO NOT want to give properties for a column but to give for another just insert and empty hash reference into the array for the column that you want to skip. This will cause the counting to proceed as expected and the properties to be applyed at the right columns.
1274 Each hashref can contain any of the keys shown below:
1278 =item B<min_w> - Minimum width of this column. Auto calculation will try its best to honour this param but aplying it is NOT guaranteed.
1280 B<Value:> can be any whole number satisfying 0 < min_w < w
1281 B<Default:> Auto calculated
1283 =item B<max_w> - Maximum width of this column. Auto calculation will try its best to honour this param but aplying it is NOT guaranteed.
1285 B<Value:> can be any whole number satisfying 0 < max_w < w
1286 B<Default:> Auto calculated
1288 =item B<font> - instance of PDF::API2::Resource::Font defining the fontf to be used in this column
1290 B<Value:> can be any PDF::API2::Resource::* type of font
1291 B<Default:> 'font' of the table. See table parameter 'font' for more details.
1293 =item B<font_size> - Font size of this column
1295 B<Value:> can be any positive number
1296 B<Default:> 'font_size' of the table.
1298 =item B<font_color> - Font color of this column
1300 B<Value:> Color specifier as 'name' or 'HEX'
1301 B<Default:> 'font_color' of the table.
1303 =item B<background_color> - Background color of this column
1305 B<Value:> Color specifier as 'name' or 'HEX'
1308 =item B<justify> - Alignment of text in this column
1310 B<Value:> One of 'left', 'right', 'center'
1316 {},# This is an empty hash so the next one will hold the properties for the second column from left to right.
1318 min_w => 100, # Minimum column width of 100.
1319 max_w => 150, # Maximum column width of 150 .
1320 justify => 'right', # Right text alignment
1321 font => $pdf->corefont("Helvetica", -encoding => "latin1"),
1323 font_color=> 'blue',
1324 background_color => '#FFFF00',
1331 NOTE: If 'min_w' and/or 'max_w' parameter is used in 'col_props', have in mind that it may be overriden by the calculated minimum/maximum cell witdh so that table can be created.
1332 When this happens a warning will be issued with some advises what can be done.
1333 In cases of a conflict between column formatting and odd/even row formatting, 'col_props' will override odd/even.
1335 =head4 Cell Properties
1337 If the 'cell_props' parameter is used, it should be an arrayref with arrays of hashrefs
1338 (of the same dimension as the data array) with one hashref for each cell of the table.
1340 Each hashref can contain any of the keys shown below:
1344 =item B<font> - instance of PDF::API2::Resource::Font defining the fontf to be used in this cell
1346 B<Value:> can be any PDF::API2::Resource::* type of font
1347 B<Default:> 'font' of the table. See table parameter 'font' for more details.
1349 =item B<font_size> - Font size of this cell
1351 B<Value:> can be any positive number
1352 B<Default:> 'font_size' of the table.
1354 =item B<font_color> - Font color of this cell
1356 B<Value:> Color specifier as 'name' or 'HEX'
1357 B<Default:> 'font_color' of the table.
1359 =item B<background_color> - Background color of this cell
1361 B<Value:> Color specifier as 'name' or 'HEX'
1364 =item B<justify> - Alignment of text in this cell
1366 B<Value:> One of 'left', 'right', 'center'
1372 [ #This array is for the first row. If header_props is defined it will overwrite these settings.
1374 background_color => '#AAAA00',
1375 font_color => 'yellow',
1382 background_color => '#CCCC00',
1383 font_color => 'blue',
1386 background_color => '#BBBB00',
1387 font_color => 'red',
1396 my $cell_props = [];
1397 $cell_props->[1][0] = {
1399 background_color => '#CCCC00',
1400 font_color => 'blue',
1405 NOTE: In case of a conflict between column, odd/even and cell formating, cell formating will overwrite the other two.
1406 In case of a conflict between header row and cell formating, header formating will override cell.
1410 my ($width_of_last_line, $ypos_of_last_line, $left_over_text) = text_block( $txt, $data, %settings)
1416 Utility method to create a block of text. The block may contain multiple paragraphs.
1417 It is mainly used internaly but you can use it from outside for placing formated text anywhere on the sheet.
1419 NOTE: This method will NOT add more pages to the pdf instance if the space is not enough to place the string inside the block.
1420 Leftover text will be returned and has to be handled by the caller - i.e. add a new page and a new block with the leftover.
1424 $txt - a PDF::API2::Page::Text instance representing the text tool
1425 $data - a string that will be placed inside the block
1426 %settings - HASH with geometry and formatting parameters.
1430 The return value is a 3 items list where
1432 $width_of_last_line - Width of last line in the block
1433 $final_y - The Y coordinate of the block bottom so that additional content can be added after it
1434 $left_over_text - Text that was did not fit in the provided box geometry.
1439 my $page = $pdf->page;
1440 my $txt = $page->text;
1449 lead => $font_size | $distance_between_lines,
1450 align => "left|right|center|justify|fulljustify",
1451 hang => $optional_hanging_indent,
1452 Only one of the subsequent 3params can be given.
1453 They override each other.-parspace is the weightest
1454 parspace => $optional_vertical_space_before_first_paragraph,
1455 flindent => $optional_indent_of_first_line,
1456 fpindent => $optional_indent_of_first_paragraph,
1457 indent => $optional_indent_of_text_to_every_non_first_line,
1460 my ( $width_of_last_line, $final_y, $left_over_text ) = $pdftable->text_block( $txt, $data, %settings );
1474 Further development since Ver: 0.02 - Desislav Kamenov
1476 =head1 COPYRIGHT AND LICENSE
1478 Copyright (C) 2006 by Daemmon Hughes, portions Copyright 2004 Stone
1479 Environmental Inc. (www.stone-env.com) All Rights Reserved.
1481 This library is free software; you can redistribute it and/or modify
1482 it under the same terms as Perl itself, either Perl version 5.8.4 or,
1483 at your option, any later version of Perl 5 you may have available.
1489 =item by Daemmon Hughes
1491 Much of the work on this module was sponsered by
1492 Stone Environmental Inc. (www.stone-env.com).
1494 The text_block() method is a slightly modified copy of the one from
1495 Rick Measham's PDF::API2 L<tutorial|http://rick.measham.id.au/pdf-api2>.
1497 =item by Desislav Kamenov (@deskata on Twitter)
1499 The development of this module was supported by SEEBURGER AG (www.seeburger.com) till year 2007
1501 Thanks to my friends Krasimir Berov and Alex Kantchev for helpful tips and QA during development of versions 0.9.0 to 0.9.5
1503 Thanks to all GitHub contributors!
1509 Hey PDF::Table is on GitHub. You are more than welcome to contribute!
1511 https://github.com/kamenov/PDF-Table