Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Creating a PDF with multiple tables and where data items can spread multipage

by tweetiepooh (Hermit)
on Sep 12, 2017 at 16:43 UTC ( #1199220=perlquestion: print w/replies, xml ) Need Help??

tweetiepooh has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I'm trying to create a PDF that will have a large number of tables present, with some data items possibly spanning pages. I can get the data into a structure I can use and have been playing around with how best to neatly present the data. The data will come from 2 sources and describes objects in each source. Objects may not all have the same data items present. The format of the output will be like

Title page for whole Section page for the data source Pages for each object, starting on a new page Item : Data Item : Data Item : Data Section page for the data source Pages for each object, starting on a new page Item : Data Item : Data Item : Data

I've managed to get the general output just by placing items on a PDF page but using a table would likely be better and there are a number of modules with examples in CPAN but many seem more focussed on row type tables

Item1 Item2 Item3 Data1 Data2 Data3 Data1 Data2 Data3 ....

Are there any tips or examples with this sort of output? Will it allow for large data (many rows) for any item. Can modules like PDF::Table handle documents with n tables (I don't know how many tables there will be? Will simply using code a bit like below work at all?

my ($table, $page, $array); while skipping through the data setting $object { $page = $pdf->page; $table = new PDF::Table $array = [] skip through data for $object setting $item and $data { process the data a little push $array,[$item,$data] } $table->table( set up table using $array ) }

Script will run on Solaris (Perl5.18) and the gen'd PDF then emailed to user. Once I get the main data working I can work on page header/footers, table of contents, indices and the like. The goal is to make the data readable so we can compare objects in each data source. Objects can be named differently and can have some small code differences but functionally should be similar. Trying to use the vendor tools is not an easy task hence trying to read out the data and "format" it.

Replies are listed 'Best First'.
Re: Creating a PDF with multiple tables and where data items can spread multipage
by roboticus (Chancellor) on Sep 12, 2017 at 17:15 UTC

    tweetiepooh:

    It looks like PDF::Table should be able to do the job. It automatically adds pages to your PDF document as it needs to. When you generate the table, it returns the reference to the last page it created, the vertical position of the end of the table (y) on the last page, and the number of pages it added. So you should be able to continue generating your document, something like:

    # all the setup stuff for PDF and PDF table for my $table (@tables) { # Implementation left as an exercise to the reader ;^) $data = make_table_data($table); # Generate the table my ($last_pg, $pg_cnt, $y) = $pdfTblGen->table($PDF, $cur_pg, $data +, %settings); if ($y < FOO) { # Not enough space on the page for the next thing, so create a n +ew page . . . } else { # Still plenty of space ... add stuff to page at $y + some whitespace ... } }

    The documentation is a bit skimpy, but there are a couple examples in the distribution you can play with. I installed it a few minutes ago, looks legit.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Creating a PDF with multiple tables and where data items can spread multipage
by Anonymous Monk on Sep 12, 2017 at 19:20 UTC
    A completely different (and, non-Perl) strategy would be to prepare the data as an XML file, then use an XSLT stylesheet (adapted from an off-the-shelf one) to transform that into a PDF document. This approach would remove the handling from Perl source-code and move it into the XSLT stylesheet document. Building programs to produce documents can send you down a rabbit hole of ever-growing program complexity. This alternative moves that complexity out to a stylesheet file and industry standard XML validation and transform tools. "DocBook," which is how O'Reilly produces all those books with animals on the cover, is a demonstration of just what XSLT can do, and it might do well for you to take a close look at that possibility.
Re: Creating a PDF with multiple tables and where data items can spread multipage
by rnewsham (Curate) on Sep 12, 2017 at 21:32 UTC

    I have had similar issues with creating complicated and variable pdf tables. I have recently been experimenting with wkhtmltopdf and so far it looks promising for my use cases. If you are familiar with generating html and using css it might be worth a look.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1199220]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-08-08 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?