Jasper Reports – Open Source Reporting Tool

JasperReports is an open source reporting engine. It provides ability to deliver rich content onto to the printer, the screen,  or into various formats such as  PDF, HTML, XLS, RTF, ODT, CSV, TXT and XML files. It is a Java library and can be used in a variety of Java-enabled applications to generate dynamic content. Its main purpose is to help create page-oriented, ready-to-print documents in a simple and flexible manner. JasperReports can also be used to provide reporting capabilities in our applications.

As it is not a standalone tool, it cannot be installed on its own. Instead, it is embedded into Java applications by including its library in the application’s CLASSPATH.

Features of JasperReports :

  • Flexible report layout.
  • Data can be presented either textually or graphically.
  • Developers can supply data in multiple ways.
  • Multiple data sources can be used to transfer data.
  • Watermarks can also be applied.
  • Sub reports can also be generated.
  • Various formats of reports can be exported.

 

Maven Dependency :

 

Working Process : 

1) Add Jasper Library  to the project .

2) Create some layout design before start reporting from Java code. Jasper’s reporting layout design is nothing but an XML file with the extension .jrxml .  JasperStudio or iReport as Gui tools can be used to visually design the JRXML file as well as textually.

3) This JRXML file is need to be compiled to generate .jasper . JRXML file can be compiled on the fly, dynamically from our Java code . We can also use iReport or JasperStudio to compile the jrxml file to create a Jasper file.

4) Once compiled and .jasper are created, we can start feeding data into the report from the Java code.

Tools that can be used to generate jrxml files : 

  1. Eclipse plugin for jaspersoft studio
  2. iReport
  3. Jaspersoft Studio

Report Template

A jrxml report template is consist of the following parts shown in the below figure.

 

Designing a Report :

We are going to create a simple jasper report design i.e jrxml file through jaspersoft studio.

You need to install the jaspersoft studion . After installation you need to follow these steps:

Step 1. Go to  File > New > others > Jaspersoft Studio > JasperReports project  

Now enter the name of the project and press enter . Your created project will now be visible into the project explorer .

Step 2. Select your created project name and right click then New > Jasper Report > Blank A4 (For practice ) > Next > enter the name of jrxml > Next  > Next > Finish

Your sample report design will be generated now with all the report design parts as shown in the below figure.

You can also switch to Source view to see the jrxml code of the default generated code of the report design. Below is the jrxml code .

 

Now you can change this report design from source view by manually writing the jrxml code or you can switch to the design view where you can drag and drop the components from the Pallet view present at the right side .

In this case we are manually writing the jrxml code . In this report design we are taking four string fields as id , name , department , email .

We will now define the field header for all the four fields in the page header part of the jrxml report.

Now we will use the above declared fields in the detail part of the jrxml report because this fields value will get repeated row by row according to the how many records that we will send from java code.

 

Final  jrxml file “demoReport.jrxml” is given below :

 

The Design version of the above generated  sample report is given below . You can check that design view by clicking the design view option in jaspersoft studio.

The Preview of the demo report that is generated above is given below. You can check the preview in the jaspersoft studio by clicking the preview button .

 

Compiling the jasper report :

We will now compile the report design i.e jrxml file . After compilation a .jasper file will be generated . For compilation we will use JasperCompileManager class and we will use its static method compileReportToFile to compile the jrxml report . During compilation if report design is not a valid design a JRException will occur.

 Below is the given program to compile the jrxml report and generate a jasper file. After compilation “demoReport.jasper” file will get generated.

 

Creating the EmployeeRecordModel Class with the four fields of type string that is declared in the demoReport .

Creating JRDataSource object by passing the list of EmployeeRecordModel object in the argument.

 

Generating the jasper Print object  :

Now generating the JasperPrint object from the compile report i.e .jasper file by calling the static fillReport method of JasperFillManager class . This method accepts three arguments .

Where , first argument is the jasperReport object . Second argument is map of parameters that we have used in our report . Third argument is JRDataSource object which is basically the the data source of the list of records having the fields that is used in the report.

Now , exporting the the report as pdf file using the JasperExportManager class.

 

Whole program performing all the operation is given below.

 

Output pdf generated is given below.

Leave a Reply