Working with JasperReports for easy report generation

JasperReports is a Java based open source report generating engine. It has great dynamic report generating capabilities that can be added to any Java based application. I have a very simple demo to show how it works.

I have created a link somewhere in my application and on the click of it, I see a pop-up window with the report PDF embedded within.

To start with, we need a .jrxml template that is externalized and every time the link is clicked, the .jrxml template is dynamically compiled to generate a .jasper file at the same external file location. So the first and foremost step is to prepare the mark-up for the report template, which can be easily designed using the iReport tool. The tool finally generates the .jrxml file based on the design. The standard JasperReport template has the following layout in the same order:

Fine, now let’s assume, I have my .jrxml file generated and looks something like:

I keep the above .jrxml file to some externalised location, let it be, D:/Documents/

Create a sample POJO, with two instance variables for subject name and marks, as:

Create another class, which generates a collection of java bean objects to be used for report generation. This class looks something like:

Now you need to create a method in the controller of your Spring MVC application, that actually serves the purpose of report generation. I give it some mapping as /jasper

Almost done, create a link somewhere on the landing page in your application as:

Male sure you include the following javascript function as well into your application:

Finally, clicking on the link, shows up the report PDF in a new pop-up window as:

Jasper Reports

If you need the same report to be exported to HTML, just modify the above controller method and add the following:

Add a new method renderHtml as:

So we have several points worth noting:

  1. The .jasper file is not kept anywhere in the JVM for any subsequent PDF generation and for now, is generated every time the link is clicked.
  2. As expected, if we modify the .jrxml template, it reflects instantly at runtime and we do not require any java changes, hence no build/deploy needed.
  3. As per the documentation available for JasperReports,  “Even though all its reporting functionality is available in this single JAR file (jasperreports-5.0.1.jar), JasperReports relies on other third-party libraries for related required functionality like XML parsing, logging, and PDF generation.”

To avoid performance hit on the application because of the .jrxml file being compiled again and again, I suggest following options to restrict the subsequent compilations:

  1. We can keep the .jasper file in cache and generate the .jasper soon after the cache expires.
  2. We can also check for the existing .jasper file at the respective externalized location. If it doesn’t exist, compile, else not.
  3. We can build a utility to explicitly compile the .jrxml template, generate the .jasper file and copy it to the respective externalized location.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.