PDF::Table(3pm) | User Contributed Perl Documentation | PDF::Table(3pm) |
PDF::Table - A utility class for building table layouts in a PDF::API2 object.
use PDF::API2; use PDF::Table; my $pdftable = new PDF::Table; my $pdf = new PDF::API2(-file => "table_of_lorem.pdf"); my $page = $pdf->page; # some data to layout my $some_data =[ ["1 Lorem ipsum dolor", "Donec odio neque, faucibus vel", "consequat quis, tincidunt vel, felis."], ["Nulla euismod sem eget neque.", "Donec odio neque", "Sed eu velit."], #... and so on ]; $left_edge_of_table = 50; # build the table layout $pdftable->table( # required params $pdf, $page, $some_data, x => $left_edge_of_table, w => 495, start_y => 500, start_h => 300, # some optional params next_y => 750, next_h => 500, padding => 5, padding_right => 10, background_color_odd => "gray", background_color_even => "lightblue", #cell background color for even rows ); # do other stuff with $pdf $pdf->saveas(); ...
For a complete working example or initial script look into distribution`s 'examples' folder.
This class is a utility for use with the PDF::API2 module from CPAN. It can be used to display text data in a table layout within a PDF. The text data must be in a 2D array (such as returned by a DBI statement handle fetchall_arrayref() call). The PDF::Table will automatically add as many new pages as necessary to display all of the data. 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. Also a (non)repeated header row with different layout properties can be specified.
See the "METHODS" section for complete documentation of every parameter.
my $pdf_table = new PDF::Table;
my ($final_page, $number_of_pages, $final_y) = table($pdf, $page, $data, %settings)
$pdf - a PDF::API2 instance representing the document being created $page - a PDF::API2::Page instance representing the current page of the document $data - an ARRAY reference to a 2D data structure that will be used to build the table %settings - HASH with geometry and formatting parameters.
For full %settings description see section "Table settings" below.
This method will add more pages to the pdf instance as required based on the formatting options and the amount of data.
$final_page - The first item is a PDF::API2::Page instance that the table ends on $number_of_pages - The second item is the count of pages that the table spans on $final_y - The third item is the Y coordinate of the table bottom so that additional content can be added in the same document.
my $pdf = new PDF::API2; my $page = $pdf->page(); my $data = [ ['foo1','bar1','baz1'], ['foo2','bar2','baz2'] ]; my %settings = ( x => 10, w => 570, start_y => 220, start_h => 180, ); my ($final_page, $number_of_pages, $final_y) = $pdftable->table( $pdf, $page, $data, %options );
Table settings
Mandatory
There are some mandatory parameteres for setting table geometry and position across page(s)
x => 10
start_y => 327
w => 570
start_h => 250
Optional
next_h => 700
next_y => 750
max_word_length => 20 # Will add a space after every 20 symbols
Default padding: 0
Default padding_* $padding
padding => 5 # all sides cell padding padding_top => 8, # top cell padding, overrides 'padding' padding_right => 6, # right cell padding, overrides 'padding' padding_left => 2, # left cell padding, overrides 'padding' padding_bottom => undef # bottom padding will be 5 as it will fallback to 'padding'
border => 3 # border width is 3 horizontal_borders => 1 # horizontal borders will be 1 overriding 3 vertical_borders => undef # vertical borders will be 3 as it will fallback to 'border'
border_color => 'red'
font => $pdf->corefont("Helvetica", -encoding => "utf8")
font_size => 16
font_color => '#333333' font_color_odd => 'purple' font_color_even => '#00FF00' background_color_odd => 'gray' background_color_even => 'lightblue'
row_height => 24
new_page_func => $code_ref
header_props => $hdr_props
column_props => $col_props
cell_props => $cel_props
cell_render_hook => sub { my ($page, $first_row, $row, $col, $x, $y, $w, $h) = @_; # Do nothing except for first column (and not a header row) return unless ($col == 0); return if ($first_row); # Create link my $value = $list_of_vals[$row-1]; my $url = "https://${hostname}/app/${value}"; my $annot = $page->annotation(); $annot->url( $url, -rect => [$x, $y, $x+$w, $y+$h] ); },
Header Row Properties
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. 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.
my $hdr_props = { font => $pdf->corefont("Helvetica", -encoding => "utf8"), font_size => 18, font_color => '#004444', bg_color => 'yellow', repeat => 1, justify => 'center' };
Column Properties
If the 'column_props' parameter is used, it should be an arrayref of hashrefs, 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. 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.
Each hashref can contain any of the keys shown below:
Example:
my $col_props = [ {},# This is an empty hash so the next one will hold the properties for the second column from left to right. { min_w => 100, # Minimum column width of 100. max_w => 150, # Maximum column width of 150 . justify => 'right', # Right text alignment font => $pdf->corefont("Helvetica", -encoding => "latin1"), font_size => 10, font_color=> 'blue', background_color => '#FFFF00', }, # etc. ];
NOTE: If 'min_w' and/or 'max_w' parameter is used in 'col_props', have in mind that it may be overridden by the calculated minimum/maximum cell witdh so that table can be created. When this happens a warning will be issued with some advises what can be done. In cases of a conflict between column formatting and odd/even row formatting, 'col_props' will override odd/even.
Cell Properties
If the 'cell_props' parameter is used, it should be an arrayref with arrays of hashrefs (of the same dimension as the data array) with one hashref for each cell of the table.
Each hashref can contain any of the keys shown below:
Example:
my $cell_props = [ [ #This array is for the first row. If header_props is defined it will overwrite these settings. { #Row 1 cell 1 background_color => '#AAAA00', font_color => 'yellow', font_underline => [ 2, 2 ], }, # etc. ], [#Row 2 { #Row 2 cell 1 background_color => '#CCCC00', font_color => 'blue', }, { #Row 2 cell 2 background_color => '#BBBB00', font_color => 'red', }, # etc. ], # etc. ]; OR my $cell_props = []; $cell_props->[1][0] = { #Row 2 cell 1 background_color => '#CCCC00', font_color => 'blue', };
NOTE: In case of a conflict between column, odd/even and cell formatting, cell formatting will overwrite the other two. In case of a conflict between header row and cell formatting, header formatting will override cell.
my ($width_of_last_line, $ypos_of_last_line, $left_over_text) = text_block( $txt, $data, %settings)
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. 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.
$txt - a PDF::API2::Page::Text instance representing the text tool $data - a string that will be placed inside the block %settings - HASH with geometry and formatting parameters.
$width_of_last_line - Width of last line in the block $final_y - The Y coordinate of the block bottom so that additional content can be added after it $left_over_text - Text that was did not fit in the provided box geometry.
# PDF::API2 objects my $page = $pdf->page; my $txt = $page->text; my %settings = ( x => 10, y => 570, w => 220, h => 180 #OPTIONAL PARAMS lead => $font_size | $distance_between_lines, align => "left|right|center|justify|fulljustify", hang => $optional_hanging_indent, Only one of the subsequent 3params can be given. They override each other.-parspace is the weightest parspace => $optional_vertical_space_before_first_paragraph, flindent => $optional_indent_of_first_line, fpindent => $optional_indent_of_first_paragraph, indent => $optional_indent_of_text_to_every_non_first_line, ); my ( $width_of_last_line, $final_y, $left_over_text ) = $pdftable->text_block( $txt, $data, %settings );
0.9.7
Daemmon Hughes
Further development since Ver: 0.02 - Desislav Kamenov
Copyright (C) 2006 by Daemmon Hughes, portions Copyright 2004 Stone Environmental Inc. (www.stone-env.com) All Rights Reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.
The text_block() method is a slightly modified copy of the one from Rick Measham's PDF::API2 tutorial at http://pdfapi2.sourceforge.net/cgi-bin/view/Main/YourFirstDocument
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
Thanks to all GitHub contributors!
Hey PDF::Table is on GitHub. You are more than welcome to contribute!
https://github.com/kamenov/PDF-Table
PDF::API2
2018-04-21 | perl v5.26.2 |