PDF::TableX - Moose driven table generation module that is uses famous PDF::API2
Version 0.012
The module provides capabilities to create tabular structures in PDF files. It is similar to PDF::Table module, however extends its functionality adding OO interface and allowing placement of any element inside table cell such as image, another pdf, or nested table.
Sample usage:
use PDF::API2; use PDF::TableX; my $pdf = PDF::API2->new(); my $table = PDF::TableX->new(40,40); # create 40 x 40 table $table ->padding(3) # set padding for cells ->border_width(2) # set border width ->border_color('blue'); # set border color $table[0][0]->content("Sample text"); # place "Sample text" in cell 0,0 (first cell in first row) $table[0][1]->content("Some other text"; # place "Some other text" in cell 0,1 $table->draw($pdf, 1); # place table on the first page of pdf $pdf->save_as('some/file.pdf');
All attributes when set return $self allowing chaining of the calls.
Following attributes take as argument either array reference with four values describing the style in each cell side in followin order [TOP, RIGHT, BOTTOM, LEFT]. Alternatively a scalar value can be provided in which case it is coerced to ARRAY REF
padding => [1,1,1,1]
# set padding for all cells $table->padding(2); # the same as $table->paddin([2,2,2,2]); # set padding of the first row $table->[0]->padding(4); # set padding of the first column $table->col(0)->padding(4); # set padding of single cell $table->[0][0]->padding(2);
border_width => [1,1,1,1]
$table->border_width(2); $table->border_width([2,3,4,5]);
border_color => ['black','black','black','black']
$table->border_color('red'); $table->border_color(['#cccccc','white','green','blue']);
border_style => ['solid','solid','solid','solid']
Currently the only supported style is 'solid'.
margin => [10/25.4*72,10/25.4*72,10/25.4*72,10/25.4*72]
Margin is used currently to determine the space between top and bottom of the page.
$table->margin(20); $table->margin([20,10,10,2]);
Following attributes require single value.
background_color => ''
$table->background_color('blue');
text_align => 'left'
Allowed values are: 'left', 'right', 'center', 'justify'
# set text align in whole table $table->text_align('left'); # set text align in single row $table->[0]->text_align('left'); # set text align in single column $table->col(0)->text_align('left');
font => 'Times'
Allowed values are the names of PDF::API2 corefonts: Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique, Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Oblique, Symbol, Times-Bold, Times-BoldItalic, Times-Italic, Times-Roman, ZapfDingbats
$table->font('ZapfDingbats');
font_color => 'black'
$table->font_color('green');
font_size => 12
$table->font_size(10);
Following attributes control placing of the table and its behaviour
width - width of the table
start_x - x position of the table
start_y - y position of the table
rows - number of table rows
cols - number of table columns
repeat_header - shall the header be repeated on every new page (default is 0, set 1 to repeat)
Set the background colors of rows. The method takes the list of colors and applies them to subsequent rows. There is no limit to style e.g. only in odd/even fashio.
# set odd and even background colors to black and white $table->cycle_background_color('black','white'); # set the background color of rows to cycle with three colors: black, white, red $table->cycle_background_color('black','white','red');
PDF::TableX uses Moose::Role(s) to define the styles and placing of the table. They can be relatively extended providing capabilites beyond those already available. Below code snipped creates the role that uses elliptical background shape instead of rectangle.
package EllipsedBackground; use Moose::Role; sub draw_background { my ($self, $x, $y, $gfx, $txt) = @_; $gfx->linewidth(0); $gfx->fillcolor('yellow'); $gfx->ellipse($x+$self->width/2, $y-$self->height/2, $self->width/2, $self->height/2); $gfx->fill(); } use Moose::Util qw( apply_all_roles ); use PDF::TableX; use PDF::API2; my $table = PDF::TableX->new(2,2); my $pdf = PDF::API2->new(); $pdf->mediabox('a4'); # set some styles $table->padding(10)->border_width(1)->text_align('center'); # apply moose roles to specific cells apply_all_roles( $table->[0][0], 'ElipsedBackground' ); apply_all_roles( $table->[0][1], 'ElipsedBackground' ); # set some content to those roles $table->[0][0]->content("Some text"); $table->[0][1]->content("Some other text"); # and finally draw it $table->draw($pdf, 1); # and save it $pdf->saveas('some/output.pdf');
Grzegorz Papkala, <grzegorzpapkala at gmail.com>
<grzegorzpapkala at gmail.com>
Please report any bugs or feature requests at: https://github.com/grzegorzpapkala/PDF-TableX/issues
PDF::TableX is hosted on GitHub https://github.com/grzegorzpapkala/PDF-TableX
Copyright 2013 Grzegorz Papkala, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install PDF::TableX, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PDF::TableX
CPAN shell
perl -MCPAN -e shell install PDF::TableX
For more information on module installation, please visit the detailed CPAN module installation guide.