PDF-ReportWriter

 view release on metacpan or  search on metacpan

lib/PDF/ReportWriter/Report.pm  view on Meta::CPAN

        }
        
        # Same as above for `page' structure
        my $page = $data->{page};
        if( ref ( $page->{header} ) eq 'HASH' ) {
                $page->{header} = [ $page->{header} ];
        }
        if( ref ( $page->{footer} ) eq 'HASH' ) {
                $page->{footer} = [ $page->{footer} ];
        }
        
        # Same as above for font structure
        if( ! ref $config->{definition}->{font} )
        {
                $config->{definition}->{font} = [ $config->{definition}->{font} ];
        }
        
        return ( $config );
}

#
# ->save( \%config [, filename] )
#
# Saves the report back to XML format.
# This is mainly an experiment. Don't know if it works correctly,
# even if the test suite seems to say so.
#
# %config = (
#
#    definition => {
#        ...,
#        info => { ... },
#        ...,
#    },
#
#    page   => {
#        header => [ ],
#        footer => [ ],
#    },
#
#    data   => {
#        fields => ...,
#        groups => ...,
#        ...
#    },
# )
#
sub save
{
        
        my($self, $cfg, $file) = @_;
        
        # $cfg is a hash:
        # $cfg = {
        #               data            ... the 'data' part of the PDF::ReportWriter object
        #               definition      ... the top-level part of the PDF::ReportWriter object ( minus data )
        
        $file ||= $self->file();
        
        my $xml = XML::Simple->new(
                AttrIndent => 1,
                ForceArray => 1,
                KeepRoot   => 1,
                NoAttr     => 1,
                NoSort     => 1,
                RootName   => 'report',
                XMLDecl    => 1,
        );
        
        my $xml_stream = $xml->XMLout($cfg);
        my $ok = 0;
        
        #warn 'opening file '.$file;
        
        if( open(XML_REPORT, '>' . $file) )
        {
                #warn 'opened file';
                $ok = print XML_REPORT $xml_stream;
                #warn 'printed '.$ok.' on it';
                $ok &&= close(XML_REPORT);
                #warn 'closed '.$ok;
        }
        
        # Report saved?
        return($ok);
        
}


1;

=head1 NAME

PDF::ReportWriter::Report

=head1 DESCRIPTION

PDF::ReportWriter::Report is a PDF::ReportWriter class that represents a single report.
It handles the conversions from/to XML to PDF::ReportWriter correct data structures,
and can provide the data to be used in the report.
XML::Simple module is used for data structures serialization to XML and restore.

This class is designed in a way that should be simple to be overloaded,
and thus provide alternative classes that load reports in a totally different way,
or supply data connecting automatically to a DBI DSN, or who knows...

=head1 USAGE

The most useful usage for this class is through the C<PDF::ReportWriter::render_report()>
call. If you really want an example of usage of standalone Report object, here it is:

        # Create a blank report object
        my $report = PDF::ReportWriter::Report->new();
        my $config;

        # Load XML report definition
        eval { $config = $report->load('/home/cosimo/myreport.xml') };
        if( $@ ) {
            # Incorrect xml file!
            print 'Error in XML report:', $@, "\n";
        }



( run in 0.607 second using v1.01-cache-2.11-cpan-39bf76dae61 )