The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME PDF::TableX - Moose driven table generation module that is uses famous PDF::API2

VERSION Version 0.01

SYNOPSIS 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');

ATTRIBUTES All attributes when set return $self allowing chaining of the calls.

Style Definitions 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

  • 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]); =back

    Following attributes require single value. =over 4 =item * 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:

              $table->font('ZapfDingbats');

      * font_color => 'black' $table->font_color('green');

      * font_size => 12 $table->font_size(10);

Placing & Behaviour Following attributes control placing of the table and its behaviour

METHODS =head2 cycle_background_color 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');

EXTENDING THE MODULE 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');

AUTHOR

Grzegorz Papkala, <grzegorzpapkala at gmail.com>

BUGS Please report any bugs or feature requests to bug-pdf-tablex at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PDF-TableX. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT You can find documentation for this module with the perldoc command. perldoc PDF::TableX

You can also look for information at: =over 4 =item * RT: CPAN's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=PDF-TableX =item * AnnoCPAN: Annotated CPAN documentation http://annocpan.org/dist/PDF-TableX =item * CPAN Ratings http://cpanratings.perl.org/d/PDF-TableX =item * Search CPAN http://search.cpan.org/dist/PDF-TableX/ =back

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE 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.

5 POD Errors

The following errors were encountered while parsing the POD:

Around line 224:

=over should be: '=over' or '=over positive_number'

Around line 271:

=over should be: '=over' or '=over positive_number'

You can't have =items (as at line 290) unless the first thing after the =over is an =item

Around line 299:

You forgot a '=back' before '=head2'

Around line 302:

=over should be: '=over' or '=over positive_number'

Around line 312:

You forgot a '=back' before '=head1'