PDF::ReportWriter 1.5

PDF::ReportWriter is a Perl module that produces high-quality PDF reports from a report definition and a data array.
PDF::ReportWriter is a Perl module that produces high-quality PDF reports from a report definition and a data array.

PDF::ReportWriter supports text formatting and alignment, unlimited grouping with group functions, intelligent page breaking, image support, color support, shaped cell backgrounds, and numeric formatting.

USAGE

The example below is purely as a reference inside this documentation to give you an idea of what goes where. It is not intended as a working example - for a working example, see the demo application package, distributed separately at http://entropy.homelinux.org/axis_not_evil

First we set up the top-level report definition and create a new PDF::ReportWriter object ...

$report = {

destination => "/home/dan/my_fantastic_report.pdf",
paper => "A4",
orientation => "portrait",
template => '/home/dan/my_page_template.pdf',
font_list => [ "Times" ],
default_font => "Times",
default_font_size => "10",
x_margin => 10 * mm,
y_margin => 10 * mm,
info => {
Author => "Daniel Kasak",
Keywords => "Fantastic, Amazing, Superb",
Subject => "Stuff",
Title => "My Fantastic Report"
}

};

my $pdf = PDF::ReportWriter->new( $report );

Next we define our page setup, with a page header ( we can also put a 'footer' object in here as well )

my $page = {

header => [
{
percent => 60,
font_size => 15,
align => "left",
text => "My Fantastic Report"
},
{
percent => 40,
align => "right",
image => {
path => "/home/dan/fantastic_stuff.png",
scale_to_fit => TRUE
}
}
]

};

Define our fields - which will make up most of the report

my $fields = [

{
name => "Date", # 'Date' will appear in field headers
percent => 35, # The percentage of X-space the cell will occupy
align => "centre", # Content will be centred
colour => "blue", # Text will be blue
font_size => 12, # Override the default_font_size with '12' for this cell
header_colour => "white" # Field headers will be rendered in white
},
{
name => "Item",
percent => 35,
align => "centre",
header_colour => "white",
},
{
name => "Appraisal",
percent => 30,
align => "centre",
colour_func => sub { red_if_fantastic(@_); }, # red_if_fantastic() will be called to calculate colour for this cell
aggregate_function => "count" # Items will be counted, and the results stored against this cell
}

];

I've defined a custom colour_func for the 'Appraisal' field, so here's the sub:

sub red_if_fantastic {

my $data = shift;
if ( $data eq "Fantastic" ) {
return "red";
} else {
return "black";
}

}

Define some groups ( or in this case, a single group )

my $groups = [

{
name => "DateGroup", # Not particularly important - apart from the special group "GrandTotals"
data_column => 0, # Which column to group on ( 'Date' in this case )
header => [
{
percent => 100,
align => "right",
colour => "white",
background => { # Draw a background for this cell ...
{
shape => "ellipse", # ... a filled ellipse ...
colour => "blue" # ... and make it blue
}
}
text => "Entries for ?" # ? will be replaced by the current group value ( ie the date )
}
footer => [
{
percent => 70,
align => "right",
text => "Total entries for ?"
},
{
percent => 30,
align => "centre",
aggregate_source => 2 # Take figure from field 2 ( which has the aggregate_function on it )
}
}

];

We need a data array ...

my $data_array = $dbh->selectall_arrayref( "select Date, Item, Appraisal from Entries order by Date" );

Note that you MUST order the data array, as above, if you want to use grouping. PDF::ReportWriter doesn't do any ordering of data for you.

Now we put everything together ...

my $data = {

background => { # Set up a default background for all cells ...
border => "grey" # ... a grey border
},
fields => $fields,
groups => $groups,
page => $page,
data_array => $data_array,
headings => { # This is where we set up field header properties ( not a perfect idea, I know )
background => {
shape => "box",
colour => "darkgrey"
}
}

};

... and finally pass this into PDF::ReportWriter

$pdf->render_data( $data );

At this point, we can do something like assemble a *completely* new $data object, and then run $pdf->render_data( $data ) again, or else we can just finish things off here:

$pdf->save;

last updated on:
May 29th, 2008, 10:28 GMT
price:
FREE!
homepage:
entropy.homelinux.org
license type:
LGPL (GNU Lesser General Public License) 
developed by:
Daniel Kasak
category:
ROOT \ Printing
PDF::ReportWriter
Download Button

In a hurry? Add it to your Download Basket!

user rating 25

3.1/5
 

0/5

Rate it!

Add your review!

SUBMIT