17
2007
Gravl: Insanely simple PDFs for Grails
And if I told you that 20 lines of code was probably plenty for fine looking PDFs? So you’ve just written a killer blog entry that legions of fan are keen to copy to their PDAs to read on the way home? Just click Gravl’s “PDF” icon and you’re off:
PDF is a visual medium, so why design everything in non-visual XML… why not design your PDF export in html (which you’ve got great tools for), then pass it through a simple library to tranform from html straight to PDF?
I was totally inspired by Josh’s article on using Flying Saucer (which is a html rendering component for Java which can read a bunch of html then render to a Swing component or to PDF).
Integrating into Gravl was a matter of creating a PdfController then passing it a relative URL to render. Because rendering can take a little time, I wrap it in cache to make it faster for the next guy:
import org.xhtmlrenderer.pdf.ITextRenderer;
class PdfController {
CacheService cacheService
def index = {
redirect(action: show)
}
private byte[] buildPdf(url) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(baos);
byte[] b = baos.toByteArray();
return b
}
def show = {
def baseUri = request.scheme + "://" + request.serverName + ":" + request.serverPort +
grailsAttributes.getApplicationUri(request)
log.debug "BaseUri is $baseUri"
def url = baseUri + params.url + "?print=true"
log.debug "Fetching url $url"
// grab pdf bytes from cache if possible
byte[] b = cacheService.getFromCache("pdfCache", 60, url)
if (!b) {
b = buildPdf(url)
cacheService.putToCache("pdfCache", 60, url, b)
}
response.setContentType("application/pdf")
response.setHeader("Content-disposition", "attachment; filename=blog.pdf")
response.setContentLength(b.length)
response.getOutputStream().write(b)
}
}
Then you just need to add yourself a link to the PDF creation…
<g:if test="${print!=true}">
<g:link controller="pdf" action="show" params="[url: entry.toPermalink('')]">PDF</g:link>
</g:if>
Now Flying Saucer is a strict xhtml renderer, so if all those <p> tags don’t terminate nicely… you need to know a few tricks. If you’re coming to my “whole nine yards” session at 2GX and I’ll let you in on a few other tricks…
Go forth and PDF with pleasure!
7 Comments + Add Comment
Leave a comment
Glen Smith
Archives
- April 2012
- March 2012
- January 2012
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
- December 2004
- November 2004
- October 2004
- September 2004
- August 2004
- July 2004
- June 2004
- May 2004
- March 2004
- February 2004
- January 2004
- December 2003
- November 2003
- October 2003
- September 2003

An article by Glen





Amazing.
Nicely done. It sure does seem simpler than loading up the entire iText library. Sorry.. I’ll miss your 2GEX … I did get to see Graeme’s talks at the SpringEX this past week. Quite good. I also demoed some Grails apps I’ve kicked around to some innocent by standards.
Very nice.
How much work would it be to bundle up the dependent frameworks and package this as a Grails plugin? In an ideal world, adding this functionality would be as simple as:
grail install-plugin pdf-generator
Cool plugin! Simple and clean!
One question: what about using acegi? I generate the PDF OK but the content of PDF is the login page and not the page I’ve requested :S
How can I workaround this issue?
Regards
@Paulo Freitas
any update on using iText on acegi? i’m having the same issue now.
what is this : url: entry.toPermalink(”) passing to controller
what is this : url: entry.toPermalink(”) passing to controller