This Jasper Plugin allows you to launch reports built using
Jasperreports
from within a Grails application. This is done by making available a new GSP tag called <g:jasperReport>.
Basically, this tag renders as a sequence of icons representing the various output formats handled by Jasper (PDF, RTF, XML, etc.)
See the examples in sections 3 and 4, below.
When the user clicks on the icon, the corresponding report is generated.
There are numerous options to control the rendering, including which of the seven possible formats to offer and how
to accompany the icons with input fields for report parameters, text labels, icon delimiters, etc.
If the <g:jasperReport> has a body, then the rendered HTML will be a <form> that includes that body.
It is assumed that the body contains input tags which will be used to provide input parameters to the report.
(The name of the input tag must match the name of the input parameter expected by the report.)
If the <g:jasperReport> does not have a body, then the rendered HTML will be just a series of one or more <a> tags.
1. Installation
Just execute the following from your application directory:
grails install-plugin jasper
What is installed:
- The jars for executing .jasper reports (already compiled) and/or compiling them from .jrxml files, on the fly, first.
- A GSP taglib for launching reports.
- Corresponding controller and service logic (that can be invoked directly)
- Seven 32x32 icons (web-app/images/icons/*.gif) .
- This demo GSP page.
2. Configuration
Option One: Create a web-app/reports folder and place your *.jasper (and/or *.jrxml) files there.
That's the default location. If you follow this convention, then there is nothing more to configure.
Option Two: In Config.groovy, set jasper.dir.reports to the location of your *.jasper files.
If that location is already nested within the web-app folder, then your reports will automatically be included in the war when you deploy to test or production.
In that case, you'll only have to set jasper.dir.reports universally.
If not, then you'll probably need to set it separately for each environment (development, test, production), and you'll likely need
to define grails.war.resources as well (with a closure containing GANT commands to copy the *.jasper files into
the staging dir so that they'll be included in the war).
Something like this:
grails.war.resources = {stagingDir ->
mkdir(dir: "$stagingDir/web-app/reports")
copy(todir: "$stagingDir/web-app/reports") {fileset(dir:"src/reports",includes:"*.jasper")}
}
environments {
development {
// relative to web-app
jasper.dir.reports = '../src/reports'
}
production {
// relative to web-app
jasper.dir.reports = 'reports'
}
}
3. Test Your Install
Type in your name and then click on any of the icons below to test your plugin installation and see a report of that type produced.
4. Some Other Examples
All Default Values
- One line across
- Vertical bar delimiters
- Description shown after last button, followed by parameters (if any).
- Description is the report "name" (in this case, "Parameter Example").
- Default controller = JasperController (provided by plugin)
- Default action = index
- CSS class for the form is jasperReport
- CSS class for each button is jasperButton
<g:jasperReport
jasper="sample-jasper-plugin"
format="PDF,HTML,XML,CSV,XLS,RTF,TEXT,ODT,ODS,DOCX,XLSX,PPTX"
name="Parameter Example" >
Your name: <input type="text" name="name" />
</g:jasperReport>
Some Layout and Presentation Options
- Buttons at end of form, rather than first
- Different delimiters before, between, and after buttons
- Description is suppressed (but the report name is still passed on to Jasper).
- Some formats not offered
- PDF format report will be displayed in the current browser window ("inline"), rather than downloaded.
<g:jasperReport
jasper="sample-jasper-plugin"
format="pdf, html, xls, rtf, text,odt,ods,docx,xlsx,pptx"
name="Report Name for Jasper Only]"
inline="true"
buttonPosition="bottom"
description=" "
delimiter="*"
delimiterBefore="((("
delimiterAfter=")))">
Your name: <input type="text" name="name" />
<br/><br/>
</g:jasperReport>
Grails-Supplied Domain Data
The following data should appear in report form when you click on any of the icon buttons below.
Person's Name | E-Mail Address |
Amy | amy@example.com |
Brad | brad@example.com |
Charlie | charlie@example.com |
Diane | diane@example.com |
Edward | edward@example.com |
- Alternative controller/action supplies the data model
- Our action then chains to the provided JasperController.index to process the reports.
<g:jasperReport
jasper="sample-list-domain-classes"
action="exampleWithData"
format="pdf, html, xml, csv, xls, rtf, text,odt,ods,docx,xlsx,pptx"
name="GORM Example"
description=" "
delimiter=" " />
You can also use more specific tags to gain greater control over more complicated forms.
<g:jasperForm controller="jasper"
action="exampleWithData"
id="1498"
jasper="w_iReport" >
..form contents..
<g:jasperButton format="pdf" jasper="jasper-test" text="PDF" />
.. other html..
</g:jasperForm>
You can also use more complex parameters in the report.
Report with graphs (JFreeChart support)
Graph support is a common and useful feature used in reports. In Jasper/IReport the default chart library is
JFreeChart.
The Grails Jasper Plugin supports JFreeChart reports. The link below shows an example of chart usage in Jasper reports.
<g:jasperReport
jasper="sample-list-domain-classes-3-chart"
action="exampleWithDataChart"
format="pdf, html, xml, csv, xls, rtf, text,odt,ods,docx,xlsx,pptx"
name="GORM chart Example"
description=" "
delimiter=" " />
Additional parameters, which are supported for some file formats, can be passed through (see jasperreport documentation for details).
Use the HTML_HEADER parameter to apply a custom html header to your output:
<g:jasperReport
jasper="sample-jasper-plugin"
format="html"
inline="true"
buttonPosition="bottom">
<input type="hidden" name="HTML_HEADER" value="<h2>-----------Header-----------</h2>" />
</g:jasperReport>
This plugin sets some default values for a bunch of these parameters. You can disable these defaults by submitting "useDefaultParameters" (Boolean values, defaults to true).
<g:jasperReport
jasper="sample-jasper-plugin"
format="html"
inline="true"
buttonPosition="bottom">
<input type="hidden" name="useDefaultParameters" value="false" />
</g:jasperReport>
5. Tag Reference
<g:jasperReport>
The jasperReport tag creates a link to a jasper report defined by the developer.
By default, the report must either reside in the "%PROJECT_HOME%/web-app/reports/" folder, or the path must be defined in
the
Config.groovy file, in the variable
jasper.dir.reports.
The following attributes can be defined in the tag:
-
jasper
(Required) The name of the report file, without the .jasper or .jrxml extension.
This is also used to set the name attribute of the generated form tag.
-
name
(Optional) A displayable version of the report name.
It appears in bold next to the submit buttons (by default).
Not to be confused with the name attribute of the resultant form tag (see the jasper attribute, above.)
-
id
(Optional) The id attribute for the form.
-
format
(Required) A list of output formats that are appropriatre for the report and are to be available to the user, separated by commas.
Case insensitive.
Spaces allowed.
This version supports PDF, HTML, XML, CSV, XML, RTF and TEXT formats.
A button for each format is created by the tag.
-
buttonPosition
Whether the submit buttons are to appear at the top or bottom of the form (before or after the body).
Choices are "top" and "bottom".
The default is "top".
-
class
(Optional) Style class to use for the form.
The default is "jasperReport".
-
height
(Optional) Height attribute for the icon img tags.
The default is none, so the images will be full size (32px).
-
buttonClass
(Optional) Style class to use for the submit buttons (which are actually <A> tags).
The default is "jasperButton".
-
delimiter
(Optional) Delimiter between icons.
If you don't want any delimiter, use a single space (" ") instead of an empty string (""), as an empty string will use the default ("|").
-
(The body of the tag)
If the tag has a body, then it becomes the body of the form.
Any HTML input elements defined in the body of the tag are sent as report parameters.
New Tags:
<g:jasperForm>
Much the same as the jasperReport tag, but gives you more control over how a form is rendered in HTML
-
jasper
(Required) The name of the report file, without the .jasper or .jrxml extension.
This is also used to set the name attribute of the generated form tag.
-
controller
(Required) The controller the form will submit to.
-
action
(Required) The action the form will submit to.
-
id
(Optional) The id attribute for the form.
-
class
(Optional) Style class to use for the form.
The default is "jasperReport".
<g:jasperButton>
The buttons that will submit a jasperForm when clicked. title can be any one of the types of reports that Jasper can output.
-
format
(Required) the name of the supported output format. ex. 'pdf' or 'PDF'
-
class
(Optional) class of the element in addition to default.
-
text
(Optional) text to be included next to button ex. 'print'.
6. Controller Action Chaining
Here is the controller code that sets up the data for the Grails-supplied data example, above.
As you can see, it then chains to the jasper/index action which handles everything else.
def exampleWithData = {
List people = [
new ExamplePersonForReport(name: 'Amy', email: 'amy@example.com'),
new ExamplePersonForReport(name: 'Brad', email: 'brad@example.com'),
new ExamplePersonForReport(name: 'Charlie', email: 'charlie@example.com')]
chain(controller:'jasper',action:'index',model:[data:people],params:params)
}
In the real world, this would be a GORM method call. Something like: List people = ExamplePersonForReport.findAll()
7. Generate report with a service call
From version 1.1 upwards it's possible to generate your report, without the controller action from above, with simple
service methods (so that you can generate your reports with a cron job in combination with the Quartz plugin).
The central element for this feature is a new wrapper class JasperReportDef. Instead of putting anything in the parameter
map you create a simple object containing the relevant data.
def reportDef = JasperReportDef(name:'your_report.jasper',
fileFormat:JasperExportFormat.PDF_FORMAT
)
As you can see there are only two required attributes. Of course you need provide the name of your report and the target file
format. All available file formats can be found in the
JasperExportFormat enum. You just have to choose one.
Additional attributes are:
- folder: the folder where you placed your reports. Defaults to /reports if unset and no global setting (jasper.report.dir in Config.groovy) exists.
- locale: Locale to use in the report generation.
- parameters: All additional parameters as a Map.
All you need to do now is to call one of the methods provided in
JasperService:
- generateReport(JasperReportDef reportDef): Generate a "normal" report.
- generateReport(List<JasperReportDef> reports): Generate a single response containing multiple reports.
Both return a
ByteArrayOutputStream with which you can do everything you want.
Full example:
class YourClass {
def jasperService
void yourMethod() {
def reportDef = new JasperReportDef(name:'your_report.jasper',
fileFormat:JasperExportFormat.PDF_FORMAT
)
FileUtils.writeByteArrayToFile(new File("/your/target/path/test.pdf"), jasperService.generateReport(reportDef).toByteArray())
}
}
8. Known Issues
- The Inline option is not working (at least, not in FF3 on the Mac).
- The new tag demonstration won't work on this page if you are using the grails UI performance plugin or if you aren't using prototype library.