Digital Solutions Blog

Dsg Subscribe Mail Web

Blogs (Digital Solutions Blog) (Digital Solutions Blog)

Dynamic Reporting using JasperReports

thumbnail

Christopher Freund
1 Year Ago

Jasper Reports

Jasper Reports is an open source Java reporting tool that generates dynamic content using XML and .jasper files and can be easily embedded into any Java application, including Liferay. It has the ability to take any kind of data source and create PDF, HTML, EXCEL and many other documents at run time. There are many third party tools that can be used to create Jasper Reports and most of them are open source and free.

Designing Tool

In order for reports to be dynamically generated, they must be defined in an XML file format called JRXML. These XML files can be hand coded or generated using a designing tool. Once the XML file is completed, it can be compiled into a .jasper file at runtime to generate the reports with the given data source. There are several tools that can be used to design these JRXML files, but I use one called iReport. This free open source tool allows users to easily create layouts that include charts, tables, images and much more. Not only does it provide a designer interface but includes an XML view and the ability to preview generated JRXML files. Most simple reports can be designed without having to modify the XML file directly, which saves the developer a lot of time and trouble.

Parameters

Jasper Reports allow users to pass values that can change the reports in several ways. These parameters can be used to modify the SQL requests by providing additional statements in the where clause. JRXML files give users the ability to pass many kinds of parameters at run time, including any Java class like Strings and Integers. This allows users to reflect dynamic data from a user or a data source on each report.

Java Code

Once the JRXML file is created, it can be compiled to produce a .jasper file, which can then be used to generate dynamic reports using the JasperRunManager. In the code snippet below, we have uploaded the .jasper file to the server and obtained the URL to the file. We then set the parameters that we wish to pass to generate our dynamic file and use Jasper Reports to convert the data to a PDF file and write it to the window, where it can be viewed and printed.

Results

Once JasperReports is embedded and the JRXML files are created, data can be written to a screen and printed out. Below is a simple graph designed using the iReport. As stated before, any data can be created as a PDF, HTML, Microsoft Excel, RTF, ODT, Comma-separated values or XML files using JasperReports. Making it one of the most powerful open source reporting tool.

Form

Talk To Our Experts