Saturday, May 14, 2011

Grails PDF output: PDF versurs Rendering plugins

I recently got a requirement where I needed to output a GSP as a PDF. This is not uncommon, and there are a couple of Grails plugins available for this. The most popular are the PDF plugin and the Rendering plugin.

This is exactly the type of thing that happens a lot in open source communities. There are often many plugins which seem to do the same thing. As a poor end "user" (a developer relying on someone else's code), pretty much the only thing you can do is download it and try it out.

I decided to try the PDF plugin. For one, it has better ratings in the "plugin rating system" of Grails. Plus, the original version was written by Glen A Smith, who's a co-author of my favorite Grails book, Grails in Action. Surely, that would be good, right?

...Here I am, 4 hours later. After twiddling with PDF for hours, I just installed and tried the Rendering plugin. Got out of the box functionality in 10 minutes. Ouch.

I had a fairly simple use case where I wanted to render the results of a partial template as a PDF. I struggled along the learning curve of the PDF plugin. I was not very impressed with the its documentation (or the fact that it polluted my project with its views as a way of providing documentation), and after figuring out different things (Did the pdfForm tag accept views in other directories, or did they need to be in the pdf/ directory? How do I pass parameters to my template? The simple case of passing primitives is covered by the demos, but what if I want to pass a domain object with pre-populated values that the template is supposed to reference?) I got stuck on it with some Null pointer issues generated by the plugin like so



I consequently tried the Rendering plugin, and got that working quickly.




Looking at the specs and code of the PDF plugin, it's not surprising. It's been around since Grails 0.6, and its code under the covers doesn't seem to be as modern or flexible as the Rendering plugin. I could be wrong, but it seems like the Rendering plugin has more community support and functionality moving forward, while still managing to get the special purpose job of PDF generation done pretty well.



For convenience, I created a sample project with both to showcase the problems I ran into. It is available here. Perhaps they stemmed from ignorance. I invite others to give it a try and respond with comments.

2 comments:

  1. Hi, could you upload again the sample project, please? The link you specified is not available. Thank you!

    ReplyDelete
  2. Yes, the example would be much appreciated.

    ReplyDelete