This tutorial will talk about producing Jasper PDF reports (or any other format you’d like) in you grails app. I took a look in grails plugins portal I found two plugins that could be used to do this. The Jasper Plugin and the DynamicJasper Plugin. Depending on what you really need you’ll choose one.
I see the JasperPlugin as a more customizable plugin since you’ll use it o link to an existing jasper report (.jrxml / .jasper) you have. You’ll have some work building it, modeling it and sometimes even “drawing” it, but if you really need to do your and just your jasper, I recommend this one (congratulations for the Brazilians responsible for this plugin).
Otherwise (and covered in this tutorial), if you just need a simple report for your domain classes (an poor-but-effective PDF view of your scaffold listing) like I need in one project here, the DynamicJasper Plugin is gonna let you rock!
It’s a simple, and versatile plugin that generate its output entirely dynamic. This means that you won’t need to open iReport and show us your drawing skills (as a good programmer, you may suck drawing!).
We’ll work only with the Entities Report that Dynamic Jasper offer us, if you need complex queries on the reports, I recommend you reading the “named reports” in the plugins official documentation.
Are you following me and reading my blog’s feed? Be the first to know when I publish some interesting article signing up to my feed and following me on twitter!
Tutorial Info
Groovy Version: 1.6
Grails Version: 1.1
Plugin Version: 0.5
Plugin Documentation: http://grails.org/plugin/dynamic-jasper
Download: source code
Basic setup
Well, our example this time will be a simple agenda, so, let’s create our agenda app, install the dynamic jasper plugin and then create our domain class with some constraints.
grails create-app agenda
grails install-plugin dynamic-jasper
grails create-domain-class Contact
Our initial Contact class will be this one
class Contact {
String name
String nickname
Date bornAt
String email
String website
String phone
String mobilePhone
String gender
static constraints = {
name(maxLength: 255)
nickname(nullable: true)
bornAt(nullable:true)
email(email:true)
website(link:true, nullable:true)
phone()
mobilePhone()
gender(inList:["M","F"])
}
}
Running the application and making it reportable
That’s it, you can run your application and test it if you want. Now we’re going to create our first report, the simplest one we can have. To do this just add this code to your domain class:
static def reportable = [:]
This map notation will tell what fields will be shown in the report and what options of it you’re configuring. As we do not specified any, all propeties will be there and the default report will be generated.
After this you can visit the report generator url at http://localhost:8080/agenda/djReport/index?entity=contact and this will generate a simple report file (no extension, you should add .pdf) of your contacts.
Very very simple, hã!
More options (personalization)
Now let’s configure some basic options of our report. First of all, I’ll not get all this properties in our agenda report, let’s get only the main fields (nick, phone, email):
def static reportable = [
columns: ['nickname', 'email', 'phone']
]
You can run again the report, it will be similar to this one:

Now, there are some other basic options you might want to configure, like the filename, the report title and other stuff:
title: The report’s title, by default if you do not set anything it will be “[entity-name] report”.
fileName: The name that the response file generated will have
columns: the columns shown
def static reportable = [
title: 'My agenda',
fileName: 'agenda',
columns: ['nickname', 'email', 'phone']
]

This three will help you start in this great plugin. Take a look in the plugin’s page to see all you can do.
It’s a simple but powerful plugin, you can adjust all the page layout, group properties, change the column titles, everything that does not involve the usual iReport drawing process.
Important Note
This note may be valid to other plugins either, but since grails 1.1, the installed plugins is not available in the project’s folder but in your HOME_DIR/.grails/1.1/projects/<project>/plugins
So, if you want to use advanced configuration of this (and others) plugin, you shoud enter its folder in ~/.grails/1.1/projects/agenda/plugins/, and get the configuration file of it, (in our case, DynamicJasperConfig.groovy) in its conf folder and save in our agenda/conf folder.
This file holds all plugin configuration and it can be used to setup the report layout and configure the named reports I said before.
Again, take a look in the plugin page and you’ll find everything you need! This is just an introduction of the plugin!
[]s,
Are you following me and reading my blog’s feed? Be the first to know when I publish some interesting article signing up to my feed and following me on twitter!