tags:

views:

2046

answers:

6

There are quite a number of modules on CPAN relating to the creation and manipulation of .pdf files, and I'm hoping this community can save me some time going down blind alleys.

I am looking to create .pdf files from scratch, with only simple formatting such as bold/italic and left/right/center justify. Being able to use a template file would nice, from an MVC perspective, but if the best module doesn't support that, I'm ok. I want the best module for my narrow problem set.

Edit: let's add the constraint that it does have to be a Perl module, if not a pure-perl solution. Thanks for answers thus far!

Update: PDF creation is one difficult problem to decide how to approach. In addition to the good suggestions here, there seems to be about 1,000 different ways to solve this, and knowing which solution(s) to invest your time in is a real challenge. It is easy to acquire dependencies on outside executables in the process of building this solution, which is why I have been favoring doing everything in Perl if possible.

I went down the road of trying to use PDF::Create but found it too limiting. You have to give coordinates to place each string of text and there is no built-in concept of text wrapping... this is all work you have to do. Impossible amount of overhead for my task.

I am now using PDF::API2, which is much more powerful than PDF::Create, but still demands the PDF be assembled at a troublingly low level. Luckily, there is some help online. See Rick Measham's excellent PDF::API2 tutorial with accompanying text_block() subroutine, which thankfully does the heavy lifting on the text wrap problem.

Unless you see another update here, this is the solution that ended up working for me.

+2  A: 

Does it have to be a Perl module? You could always use LaTeX and convert that to PDF. Not quite as straight-forward, but it is another option.

gms8994
As a plus, you will produce beautifully typeset documents.
Jon Ericson
I have used this method to create fairly complicated documents. In fact, you can create bits and pieces of the PDF in various applications and combine their output using http://search.cpan.org/~larslund/PDF-Reuse-0.35/
Sinan Ünür
A: 

Yeah, tough to answer without knowing exactly what your constraints are. If pure-Perl is not a necessity, I'd be inclined towards DocBook.

The initial markup you'll generate can be very simple XML; and the transformation requires just an XSL processor and shelling out to something like Apache's FOP.

Paul Roub
+3  A: 

Three modules for creating PDF come to mind (in no particular order)....

PDF::Template gives you that template option you maybe hankering for?   PDF::Create seems more straightforward (at least from the docs) and may meet your "simple formatting" requirement more adequately.

However if you want to know what the "community" thinks then only PDF::API2 gets a rating on CPAN Ratings coming in with 4 out of 5 stars overall score.

Hope that helps.

/I3az/

PS. Disclaimer: I've not used any of these modules. In past I've always gone for XML/XSLT/XSL-FO using Apache FOP with Perl being used to create the initial XML data. This can be an overkill for something small and not always ideal if you want to embed PDF generation into your Perl app.

PPS. So I'll also be looking at these CPAN PDF modules at some point in near future!

draegtun
+2  A: 

I'm the author of the CPAN module CAM::PDF which is definitely not the best tool for this job -- it's designed for high-performance editing, not creating.

Among free PDF creation libraries, I like PDF::API2 the best. It has a very rich feature set and good encryption support (inspired by CAM::PDF I might add!) The author, Alfredo, manages a popular email list. People sometimes complain about documentation, but I've found it to be adequate.

Among commercial libraries, I've had good experiences with pdflib.

Chris Dolan
The documentation is atrocious. I suspect, though, that once you get to grips with it, you no longer remember how frustrating it was to get to grips with it, and so nobody ever does anything about the documentation.
Sam Kington
Good point. :-)
Chris Dolan
Alfredo no longer maintains the module .. he's moved on from perl and so the module is unmaintained. I'm considering taking it over so that SOMEONE is doing something with it.
RickMeasham
@RickMeasham: I didn't know that -- I'm further out of date than I realized... If you do take over and have specific questions, please feel welcome to email me.
Chris Dolan
+2  A: 

G'day Marcus,

Glad you found the tutorial. I do a lot of work in PDF::API2, so if there's anything I can help with, just let me know.

Naturally, I recommend PDF::API2!

There's a guy Jay Hannah, who's currently turning the text block into a module for CPAN that does exactly what you want: bold, italic, etc. If you check the mailing list, you'll see his posts at the top.

Cheers!

Rick Measham

RickMeasham
Thanks so much for touching base with me, Rick. I just joined the Yahoo group mailing list 'perl-text-pdf-modules', which I hope is the one you meant?
Marcus
Yup, that's the one -- Cheers!
RickMeasham
A: 

how to save a online pdf file using perl? http://www.nwcc.bc.ca/FNC/pdfs/Stepping%20Stones%20to%20improved%20Relationships%20-%20web.pdf

I am using file::download. but the problem is its not downloading url with url encoded strings.

sharma

solved by using $mech->get( $uri, ':content_file' => $tempfile );sharma