15

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.

Marcus
  • 5,772
  • 8
  • 35
  • 60
  • I agree about using PDF::API2 (I was trying to steer u in that general direction!). Well done for posting followup and tutorial link. – draegtun Feb 25 '09 at 15:16

6 Answers6

7

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
  • 8,905
  • 2
  • 35
  • 73
  • 1
    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 Feb 18 '09 at 23:50
  • 2
    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 Apr 02 '09 at 12:13
  • @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 Apr 03 '09 at 03:53
  • 1
    @ChrisDolan: So can anybody point out this encryption support? I grepped the source code 'till I was blue in the face, and I don't see any other than a boolean function to tell whether the open PDF is encrypted or not. – kovacsbv Jan 27 '14 at 20:33
  • @kovacsbv - huh, wow, you're right. That's really strange. I've been telling people for years that PDF::API2 has decryption, incorrectly. It's been so long that I don't recall how I came to that conclusion. Perhaps Fredo started building the encrypt code but never finished?? I really can't say... – Chris Dolan Jan 27 '14 at 21:00
  • @ChrisDolan does this module support Google Noto fonts ? https://www.google.com/get/noto/ Struggling to find the support for NOTO fonts in perl – vsingh Apr 10 '15 at 12:58
  • @vsingh - CAM::PDF has almost zero font support. That's not it's purpose. I think you'll want a different tool if you want to, say, swap fonts in a document or add new text in a specific font. – Chris Dolan Apr 10 '15 at 19:46
  • Many years now since I started using it but PDF::Reuse has served me well in the past for Merge Data With Forms applications. I have used your CAM::PDF module @ChrisDolan to solve a very tricky high volume document manipulation and merging problem. I was thinking of seeing if I should replace my PDF::Reuse with a newer/better tool. Is PDF::API2 worth looking at as a replacement/improvement? – Arthur Nicoll Nov 19 '18 at 22:42
6

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.

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!

AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71
  • I have used `PDF::API2`, its better than `PDF::Create` in terms that you can reuse a pdf to generate the new ones. :) – Kamal Nayan May 10 '17 at 08:47
5

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.

Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
2

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
  • 1,658
  • 3
  • 14
  • 24
0

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
  • 36,322
  • 27
  • 84
  • 93
-2

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 –  Apr 06 '09 at 12:04