Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Simple Perl PDF Creation

by jeffthewookiee (Sexton)
on Dec 13, 2011 at 16:54 UTC ( #943386=perlquestion: print w/replies, xml ) Need Help??

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

Seeking a solution for a Catalyst application that needs to generate PDFs. There are some CPAN modules available but they all seem pretty heavyweight. Would like something that I can just hand a "PDF Template" with 3 or 4 dynamically generated values that would be used in the final PDF...

Is there a tool out there that will work along the lines of Template::Toolkit only for PDF? Would like to avoid having to learn the inner workings of PDF to generate it in Perl.

Replies are listed 'Best First'.
Re: Simple Perl PDF Creation
by juster (Friar) on Dec 13, 2011 at 18:28 UTC
    PDF::Reuse is probably the simplest game in town. It's pure perl, "fast", has only one module, and is not OO. No it's not like TT. When I used it I calculated the position of each line to place the text.
      PDF::Reuse is very strong and easy to work with if you add some (even readonly) form fields to your document and just fill in their values. Users can't change them (because they're readonly), but if you wanna change just a few lines, try it out.

      I'll put in another vote for PDF::Reuse. It's lightweight from the perspective of:

      • few dependencies
      • quick to load
      • very fast at runtime
      • API is small

      The interface is nothing like Template Toolkit. Instead you call functions to select a font and then write text to particular X, Y coordinates. The text can be aligned left, right or centered but if you need to wrap text in a box then you'll have to do the hard work yourself - which might make a different module a better choice. Also if you want to draw lines or boxes then the API really just lets you inject the relevant raw PDF commands - which some people might consider a negative.

Re: Simple Perl PDF Creation
by MidLifeXis (Monsignor) on Dec 13, 2011 at 17:28 UTC

    PDF::Template is one I have used in the past. There has been some movement on it in the last few years, however, so I am not sure what the current state of it is.


Re: Simple Perl PDF Creation
by choroba (Archbishop) on Dec 13, 2011 at 17:03 UTC
    For simple PDF's, I usually generate LaTeX sources with Perl and then compile them to PDF's through pdflatex.
Re: Simple Perl PDF Creation
by chrestomanci (Priest) on Dec 13, 2011 at 21:34 UTC

    I posted a question about pdf generation from templates about a year ago, it might be worth reading the answers I got.

    My eventual solution was to to go via SVG. I prepared an SVG template by drawing it in a graphics program and then opening the SVG file in an editor and replacing elements with template tags. I then filled in the template with perl Text::Template (I dare say other templating systems such as Catalyst's favourite Template::Toolkit would work as well). Once I had an SVG file I fed it through svg2pdf (that comes with Inkscape) to convert to PDF.

    Bear in mind that my project involved a lot of vector & bitmaped graphics, and not a lot of text. If you need to put a lot of text into your PDF files then my solution may not work as well as you are likely to have to do things like line breaks yourself.

Re: Simple Perl PDF Creation
by trwww (Priest) on Dec 13, 2011 at 17:53 UTC

    What I settled on is HTML::HTMLDoc. It turns HTML documents in to PDF. This way I can serve a html version of a datagroup, and then if I want a "hard copy" I can pdf the html.

    There are a few caveats. You have to have the underlying c htmldoc library installed so theres a bit of maintenance involved getting it set up and/or moving your app around, and for me I had to settle on htmldoc-1.8.27 because newer versions didn't give me the pdf formatted how I wanted.

    But if you can deal with those it is a nice tool to get .pdf of your documents

Re: Simple Perl PDF Creation
by xorl (Deacon) on Dec 13, 2011 at 20:30 UTC
    What I'd suggest is using a pdf form. Create one where everything is already set except for your 3 or 4 dynamic fields. In perl create a related xfdf file (xfdf is a lot easier than fdf since it is an xml based file). Then make a system call to pdftk ( to merge the data into the form and output the pdf with your dynamic data.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://943386]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-05-17 04:49 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (154 votes). Check out past polls.