Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Dynamic tables using PDF::Template

by mhearse (Chaplain)
on Jan 09, 2008 at 10:11 UTC ( #661299=perlquestion: print w/replies, xml ) Need Help??

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

I've got a working program which generates tabular pdf output dynamically. The only issue I'm having now is determining the column (textbox) length. It can't be fixed as a given report may have more/less columns. Also, I don't need a wide column for data which is only three characters. Currently I iterate over the data set, getting the max/average length of characters in a column. How can I translate this to textbox width? Also, is it possible to use width percentage for a situation such as this? Any help is appreciated.

Replies are listed 'Best First'.
Re: Dynamic tables using PDF::Template
by dragonchild (Archbishop) on Jan 09, 2008 at 14:20 UTC
    Wow. Two questions and two doozies. Dynamically determining column width from data is really really difficult, particularly for outputs which are paginated, dynamically formatted, and printed. It could've been fixed pretty easily, except for the fact that PDF::Template is a single-pass transformer. All the fixes require a two-pass engine and rewriting the engine just for this situation was deemed too much work for the effort.

    The best solution I've found is to use percentages and keep tweaking. Alternately, you can actually have the widths be variable and calculate things in your code.

    As for more permanent solutions, take a look at Excel::Template::Element::Cell::AutoSize. While it's for Excel::Template and not PDF::Template, they were written off the same engine (E::T is just a rewrite of P::T) and so the technique of adding your own elements dynamically works the same for both.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Dynamic tables using PDF::Template
by stvn (Monsignor) on Jan 09, 2008 at 14:25 UTC

    You may need to drop into plain PDFLib to do this, IIRC the latest version supports a table-type layout, not sure how much automatic formatting it will do for you. You can always concat two (or more) PDFs using CAM::PDF if you find that you need to generate them seperately.

    How can I translate this to textbox width?

    PDFLib supplies a way to get the width of a given string using the currently selected font, I don't recall the function name right now, but it should be easy enough to find looking at the manual.

      Thanks for your replies. I am able to use the PDFLib method
      $pdf->string_width(text => 'text')
      On another note, does anyone know if I can set the pdf width? Thus far I see only page types (A3, A4, etc). Thanks again.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2021-10-17 06:04 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (71 votes). Check out past polls.