Wednesday, January 1, 2014

using jasper report for Grails

Jasper reporting library is very popular in JAVA community. And it can be used with Grails. You can install plugin with  

                                                            grails install-plugin jasper

in command prompt of Grails. and get a coffee because its going to take time. after jasper is installed in your project. you have to create a .jrxml file or .jasper file. you can create these files with tools like iReport or some other are there. you can download iReport here http://sourceforge.net/projects/ireport/files/iReport/iReport-5.1.0/ and there you can design your .jrxml file or .jasper file very easily. i will be writing more blogs about designing reports in iReport. 

from there you will have jrxml file . you have to put the file that you create in dir ==> your_app\web-app\reports. you have to create a reports folder in web-app and paste it there. Okay now we go back to our grails project. 

now in your gsp copy and paste following code

<g:jasperReport
          jasper="file name without extension"
          format="PDF,HTML,XML,CSV,XLS,RTF,TEXT,ODT,ODS,DOCX,XLSX,PPTX"
          name="name of the report">
    Your name: <input type="text" name="name"/>
  </g:jasperReport>

here name parameter is sent to the .jasper file. this is like if you send "aaditya" as name in gsp you can print this name in report. 

and you will see a icon on the page you located the above code to get the report. 
Attributes:
  • jasper - filepath, relative to your configured report folder, of the report, no file extension needed (Required)
  • format - supply the file formats you want to use in a simple list (Required)
  • name - name of the report
  • delimiter - delimiter between the icons representing the file formats.
  • delimiterBefore - delimiter in front of the icons
  • delimiterAfter - delimiter at the end of the icons
  • description - description of the report
  • buttonPosition - position of the icons (top or below)
with jasperButton and jasperForm you can get more control in you data to be reported:
<g:jasperForm controller="report"
    action="jasper"
    id="1498"
    jasper="w_iReport" >
..form contents..
<g:jasperButton format="pdf" jasper="jasper-test" text="PDF" />
.. other html..
</g:jasperForm>

okay you need a action code . here is a sample that i used. :

 def jasper () {
          def emp = Employee.findAll()
        chain(controller:'jasper', action:'index', model:[data:emp],params:params)
    }

here controller is report and action is jasper. here emp stores all the data that i need to report.

0 comments:

Post a Comment