Showing posts with label japser. Show all posts
Showing posts with label japser. Show all posts

Wednesday, September 9, 2009

Creating jasper reports in Grails

I like to work with Grails, so to continue making useful stuff i elaborated a little bit about how to make reports in Grails using Japser (which is a nice reporting tool)

My aim is to do reports in PDF and Excel. And since it don't think it is the f...ing business of the report file to know anything about the database I intend to use my own models to send into the report.

Create an application:
 grails create-app bookshelf

Install the plugin:
 grails install-plugin jasper
This usually takes a while since Plugin list cache is almost always out of date when installing plugins. So go take a coffe or something.

First of all I want a domain-class to hold books. So create that one and set up a few books in the BootStrap.groovy to have somthing to test with.
 grails create-domain-class Book


Add a few properties to the book, for example:

and add a few books in the BootStrap.groovy to have something

To create a report from Jasper, you need to create a .jrxml and then compile it to.jasper report file.


To create a report for my purpose, I used iReport. this is a tricky one because the jasper plugin which I got when installing is 0.9.7 and has a mistake in it that makes the reports fail. However to solve that look up the lib folder in the plugin-installment (using grails1.0.4 is in the project, but using 1.1.1 it ends up in your home folder) and remove the jasperreports-2.0.5.jar, otherwise you will end up with some NullPointerExceptions when tryring to run the report. Loocking there you see that the other jar file is jasperreports-3.1.2.jar, so the iReport version to download is 3.1.2.

I choosed the standalone version but there is a version that plugs into Netbeans.

Making the following report and compile it makes an .jasper file to use from grails.



Now things is starting to get a little bit interesting. We now need to make a report of this. I genereated all stuff for book by using the command:
 grails generate-all Book
we can then use the generated views to present the report.

Add the following line in the view to get a PDF and a XLS report:


(of course they should be tagged, but this stupid blog editor does not want to show it if if make them as tags)

for example under pagination, I used an action in the controller and a little bit of of code, not much really, to to what is needed.

That's it. Try it out yourself!

(For  the lazy one, a zip of the project can be found here: http://sites.google.com/site/ironicprogrammer/home/grails-and-jasper-1 as an attachement)