Grails: How do I render GSP files in PDF format?

Playing with Grails Rendering Plugin with 현종서.
Hello there!~ In this tutorial, we will be playing with the Grails rendering plugin. This plugin obviously allows you to render pdf versions of your gsp files. I actually found it very easy to use, and I think you will too! But, before we get started, I will tell you the versions I used, just to be consistent.

Other Information
Installing the rendering plugin
Installation is very easy. Open your BuildConfig.groovy and add 'runtime ":rendering:0.4.3"' under your plugins.
plugins {
runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.7.1"
runtime ":resources:1.1.5"
compile  ":rendering:0.4.3"
build ":tomcat:$grailsVersion"
}
After adding that one line, simply run the script below:
grails refresh-dependencies
I actually prefer this method of installation because I can keep an easy track of the versions I'm using. Also, it's good to check if the plugin was installed successfully. To get the list of plugins installed, simply run the script below. It will give you an informative list of plugins you have installed and their version numbers.
grails list-plugins -installed
It will give a result similar to this. Make sure that you see the rendering plugin! If you don't see the rendering plugin in the list, then there must something wrong with the installation.
Domain Setup
Okay, I created a domain called profile. It's just a sample domain which features a date, a string, a boolean and a byte[] for photos. This will maximize our knowledge on how to use the rendering plugin with different data types.
class Profile {
String firstName
String lastName
Date birthdate
boolean isAvailable

byte[] photo

static mapping = {
photo(sqlType: 'longblob')
}

static constraints = {
firstName (blank:false, maxSize:50, matches: '[A-Za-z]+')
lastName (blank:false, maxSize:50,matches: '[A-Za-z]+')
}

String getFullName(){
"${firstName} ${lastName}"
}
}
The Controller
In our controller, define the pdfRenderingService and then create a method(renderFormPDF()) that will render the form for us. Take note that you will need a _pdf.gsp for the template!
def pdfRenderingService
def renderFormPDF(){
def formInstance = Profile.get(params.id)
def args = [template:"pdf", model:[form:formInstance]]
pdfRenderingService.render(args+[controller:this],response)
}
The Views
Next, we create a template and we call it _pdf.gsp. It can actually be any name as long as it is a template, and it should match the template on our renderFormPDF() method in our controller. The underscore sign "_" is necessary for it to qualify as a template. Below are some of the snippets for getting the values out of the passed model. :)

Rendering Images from the Domain Using Rendering Plugin
<rendering:inlineJpeg bytes="${form?.getPhoto()}" height="200px"/>

Rendering Dates from the Domain Using Rendering Plugin
<g:formatDate date="${form?.getBirthdate()}" format="dd MMM yyyy"/>

Rendering Boolean values Using Rendering Plugin
<g:formatBoolean boolean="${form?.isAvailable}" true="Yes! I'm single!" false="No! I'm in a relationship."/>
It's really just straight forward! You can use existing grails tags here which makes it more convenient. Now, one question you may ask is: How do I adjust the width and height of the pdf rendered? The answer lies below. For instance you want it to be the size of small bond paper, you can simply add the snippet below in your css.
@page {
size: 8in 11.5in; /* width height */
margin: 0.25in;
}
How do I render images with fixed/dynamic paths using rendering plugin?
If you have an image which does not live inside a database (meaning it is contained somewhere inside your project folder), you can simply get the file path and return it as bytes in your controller. Let me demonstrate.
In the controller:
def logo = new File(ApplicationHolder.application.parentContext.servletContext.getRealPath("/images/myPicture.jpg"))
def args = [template:"pdf", model:[logo: logo.bytes]]
pdfRenderingService.render(args+[controller:this],response)

In the view:
<rendering:inlineJpeg bytes="${logo}" height="200px"/>

That's about it! Don't forget to like us on facebook! If you have more questions, please feel free to comment below! 안녕! :)

POSTED BY 현종서
DISCUSSION 2 Comments

2 Responses to : Grails: How do I render GSP files in PDF format?

  1. hyori says:

    It works! Awesome! :D

  2. pusye2n3 says:

    Thank you so much on thr hint about returning it in BYTES. It works now.

Leave a Reply

Got something to share?
Let me know in the comment section below.

Powered by Blogger.