PDF::Table(3)

NAME
PDF::Table - A utility class for building table layouts in a PDF::API2 object.

SYNOPSIS
 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 => 750,
     next_y  => 700,
     start_h => 300,
     next_h  => 500,
     # some optional params
     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();
...

EXAMPLE
For a complete working example or initial script look into distribution`s 'examples' folder.

DESCRIPTION
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 the 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.

METHODS

  new
    Returns an instance of the class. There are no parameters.

  table($pdf, $page_obj, $data, %opts)
    The main method of this class. Takes a PDF::API2 instance, a page instance, some data to build the table and formatting options. The formatting options should be passed as named parameters. This method will add more pages to the pdf instance as required based on the formatting options and the amount of data.

    The return value is a 3 item list where The first item is the PDF::API2::Page instance that the table ends on, The second item is the count of pages that the table spans, and The third item is the y position of the table bottom.

Example:
     ($end_page, $pages_spanned, $table_bot_y) = $pdftable->table(
         $pdf,               # A PDF::API2 instance
         $page_to_start_on,  # A PDF::API2::Page instance created with $page_to_start_on = $pdf->page();
         $data,              # 2D arrayref of text strings
         x  => $left_edge_of_table,    #X - coordinate of upper left corner
         w  => 570, # width of table.
         start_y => $initial_y_position_on_first_page,
         next_y  => $initial_y_position_on_every_new_page,
         start_h => $table_height_on_first_page,
         next_h  => $table_height_on_every_new_page,
         #OPTIONAL PARAMS BELOW
         max_word_length=> 20,   # add a space after every 20th symbol in long words like serial numbers
         padding        => 5,    # cell padding
         padding_top    => 10,   # top cell padding, overides padding
         padding_right  => 10,   # right cell padding, overides padding
         padding_left   => 10,   # left cell padding, overides padding
         padding_bottom => 10,   # bottom padding, overides -padding
         border         => 1,    # border width, default 1, use 0 for no border
         border_color   => 'red',# default black
         font           => $pdf->corefont("Helvetica", -encoding => "utf8"), # default font
         font_size      => 12,
         font_color_odd => 'purple',
         font_color_even=> 'black',
         background_color_odd  => 'gray',         #cell background color for odd rows
         background_color_even => 'lightblue',     #cell background color for even rows
         new_page_func  => $code_ref,  # see section TABLE SPANNING
         header_props   => $hdr_props, # see section HEADER ROW PROPERTIES
         column_props   => $col_props, # see section COLUMN PROPERTIES
         cell_props     => $row_props, # see section CELL PROPERTIES
     )

HEADER ROW PROPERTIES
    If the 'header_props' parameter is used, it should be a hashref. It is your choice if it will be anonymous inline hash or predefined one. Also as you can see there is no data variable for the content because the module asumes that the 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.

    $hdr_props =
    {
        # This param could be a pdf core font or user specified TTF.
        #  See PDF::API2 FONT METHODS for more information
        font       => $pdf->corefont("Times", -encoding => "utf8"),
        font_size  => 10,
        font_color => '#006666',
        bg_color   => 'yellow',
        repeat     => 1,    # 1/0 eq On/Off  if the header row should be repeated to every new page
    };

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:

  $col_props = [
    {},# This is an empty hash so the next one will hold the properties for the second row from left to right.
    {
        min_w => 100,       # Minimum column width.
        justify => 'right', # One of left|right ,
        font => $pdf->corefont("Times", -encoding => "latin1"),
        font_size => 10,
        font_color=> 'blue',
        background_color => '#FFFF00',
    },
    # etc.
  ];

    If the 'min_w' parameter is used for 'col_props', have in mind that it can be overwritten by the calculated minimum cell witdh if the userdefined value is less that calculated. This is done for safety reasons. In cases of a conflict between column formatting and odd/even row formatting, the former will override the latter.

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 keys shown here:

  $cell_props = [
    [ #This array is for the first row. If header_props is defined it will overwrite this settings.
      {#Row 1 cell 1
        background_color => '#AAAA00',
        font_color       => 'blue',
      },
      # etc.
    ],
    [ #Row 2
      {#Row 2 cell 1
        background_color => '#CCCC00',
        font_color       => 'blue',
      },
      {#Row 2 cell 2
        background_color => '#CCCC00',
        font_color       => 'blue',
      },
      # etc.
    ],
        # etc.
  ];

    In case of a conflict between column, odd/even and cell formating, cell formating will overwrite the other two. In case of a conflict between header row cell formating, header formating will win.

TABLE SPANNING
    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. For example you can use it to put Page Title, Page Frame, Page Numbers and other staff that you need. 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. Dont forget that your function must return a page object created with PDF::API2 page() method.

  text_block( $txtobj, $string, x => $x, y => $y, w => $width, h => $height)
    Utility method to create a block of text. The block may contain multiple paragraphs. It is mainly used internaly but you can use it from outside for placing formated text anywhere on the sheet.

Example:

     # PDF::API2 objects
     my $page = $pdf->page;
     my $txt = $page->text;

     ($width_of_last_line, $ypos_of_last_line, $left_over_text) = $pdftable->text_block(
        $txt,
        $text_to_place,
        #X,Y - coordinates of upper left corner
        x        => $left_edge_of_block,
        y        => $y_position_of_first_line,
        w        => $width_of_block,
        h        => $height_of_block,
        #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,
     );

AUTHOR
Daemmon Hughes

DEVELOPMENT
ALL IMPROVEMENTS and BUGS Since Ver: 0.02

Desislav Kamenov

VERSION
0.9.3

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

PLUGS
by Daemmon Hughes

Much of the work on this module was sponsered by Stone Environmental Inc. (www.stone-env.com).

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

by Desislav Kamenov

The development of this module is sponsored by SEEBURGER AG (www.seeburger.com)

Thanks to my friends Krasimir Berov and Alex Kantchev for helpful tips and QA during development.

SEE ALSO
PDF::API2