Dynamic Jasper with Example

Dynamic Jasper Report  – Making dynamic reports easy

DynamicJasper (DJ) is an open source free library that hides the complexity of Jasper Reports, it helps developers to save time when designing simple/medium complexity reports generating the layout of the report elements automatically. DJ creates reports dynamically, defining at runtime the columns, column width (auto width), groups, variables, fonts, charts, crosstabs, sub reports (that can also be dynamic), page size and everything else that you can define at design time.

DJ keeps full compatibility with Jasper Reports since it’s a tool that helps creating reports in a programmatic friendly way, taking care of the report design for you.

Classic .jrxml files can be used as templates while the content and layout of the report elements are handled by the DJ API.


100% pure Java

  • No need to use other tool than you favorite IDE.
  • Friendly and intuitive API.
  • Mature, robust and stable.

Dynamic column report: Columns can be defined at runtime, which means you also control (at runtime) the column positioning, width, title, etc.

Repeating groups / Breaking groups: Create repeating groups dynamically using simple expressions as criteria or complex custom expressions. Each repeating group may have a header and/or footer, which can have a variable showing the result of an operation (SUM, COUNT or any other provided by Jasper Reports).

Automatic report layout: Just define a minimum set of options and DJ will take care of the layout. It’s not an issue to generate the same report for different page sizes and orientation many more!

Dynamic Crosstabs: Jasper Report’s popular crosstabs can now be created dynamically in an easy and convenient way.

Sub reports

  • Sub reports are supported; they can also be dynamically created.
  • Concatenating many reports in a single one (e.g.: a single PDF) can be a hard task. Using DynamicJasper it is really easy get reports of different nature in a single one.

Styles: Each column can have its own style for its title and detail data (defining border, border color, font size, type and color, background color, etc.).

Style library from jrxml files are supported.

Calculation Variables: Repeating groups can have variables that hold the result of an operation on a given field (column). With DJ adding variables is a 1 line of code task.

JRXML template files support: You can use a base template jrxml file in which common styles, company logo, water mark, etc can be pre defined.

Conditional Format: DJ provides a very simple way to define conditional formats. You can use simple conditions or custom conditions.

Auto text: Add auto text in page header and footer such as ?Page 1 of 10?, ?Generated on Oct. 10th 2007? or a custom text.

Charts: Easy to add simple charts.

Barcode columns: As simple as adding a regular column.

Export to most popular formats: As DJ stands over Jasper Reports, it can export to PDF, XML, HTML, CSV, XLS, RTF, TXT.

Clean Excel export: One of the most valuable features that DJ provides is exporting plain reports to excel, with no data formatting, no page break, etc. This is very valuable for end users that use this report to create dynamic tables in Excel, creating these reports just with Jasper Reports can demand a lot of design time.

Integration with popular frameworks:

  • Struts 2 and WebWork are supported out of the box.
  • Grails plug-in is soon to be released.

package com.javamagic.util;

import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.ReflectiveReportBuilder;

public class DynamicJasperReport extends HttpServlet  {

private static final long serialVersionUID = 1L;

public String getDynamicJasperReport(String query, String title, String fileName, HttpServletResponse resp){

Session session = null;
String result = "";
try {

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();

List list = session.createQuery(query).list();

DynamicReport dynamicReport = new ReflectiveReportBuilder (list).build();

JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, new ClassicLayoutManager(), list);

resp.setHeader("Content-Disposition", "attachment;filename=\""+fileName+".pdf\"");

JasperExportManager.exportReportToPdfStream(jasperPrint, resp.getOutputStream());

result = "success";
} catch (Exception e) {
System.out.println("Error : "+e);
result = "error";
} finally {
if (session != null) {

return result;


Now we can call that  Dynamic Jasper function from any action/servlet.

public String getDynamicJasper(){
 String result = "";

 String query = "from ExamPaper";
 String title = "List of Exam Paper";
 String fileName = "ExamPaperList";

DynamicJasperReport  djr = new DynamicJasperReport();

result = djr.getDynamicJasperReport(query, title, fileName, getResponse());
 System.out.println("result : "+result);

}catch(Exception e){
 System.out.println("Error in getDynamicJasper : "+e.getMessage());
 result = "error";
 return result;

Hope you will enjoy. Cheers…. 🙂


Call Jasper Report / Ireport From Java Application

JasperReports is an open source Java reporting tool that can write to screen, to a printer or into PDF, HTML, Microsoft Excel, RTF, ODT, Comma-separated values and XML files.

It can be used in Java-enabled applications, including Java EE or Web applications, to generate dynamic content. It reads its instructions from an XML or .jasper file dynamically.

We can generate reports using two ways,

1) From “JRXML” (Source) file
2) From “Jasper” (Compiled) file

Following jar must be in classpath

  1. commons-beanutils-1.8.2.jar
  2. commons-collections-3.2.1.jar
  3. commons-digester-1.7.jar
  4. commons-logging-1.1.jar
  5. groovy-all-1.7.5.jar
  6. iText-2.1.7.jar
  7. jasperreports-4.1.1.jar

Note : iReport for designing the report.

Create PDF Report From JRXML File

JRXML file is a JasperReports Document. JasperReports are defined in an XML file format, called JRXML, which can be hand-coded, generated, or designed using a tools like IReport, JasperAssistant etc

Execution of report from JRXML file will be very slow, as it need to compile before the execution

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromXmlFile {
public static void main(String[] args) throws JRException, IOException {

JasperReport jasperReport = JasperCompileManager.compileReport("report.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap(), new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");

Create PDF Report From Jasper File

Jasper file is a compiled format of JasperReports Document.

Execution of report from Jasper file will be very fast, as it is pre-compiled

It is recommended for the production environment

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromJasperFile {
public static void main(String[] args) throws JRException, IOException {

JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper",  new HashMap<String, Object>(),
new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");


Hope you will like this. Cheers… 🙂

Jasper Report vs iBIRT Report

BIRT is the only reporting tool that is part of the Eclipse foundation. That alone is enough for me. The IDE is easy-to-use and you can design reports using a really intuative WYSIWYG designer. When things get more complex, you have the APIs and scripting hooks to handle it. After you design the report itself, you have a ton of deployment flexbility.

My short list of reasons I would go with BIRT:

  1. Data agnostic designer: JDBC, Native Drivers, Web Services, POJOs
  2. Eclipse-based: The community in and around BIRT & Eclipse is thriving.
  3. Drag-andDrop report creation: No programming is required to create content
  4. Versatility: You can create all kinds of content with BIRT, not just traditonal BI.
  5. Easy to deploy: Eihter embedded in an application or stand-alone
  6. Once executed and built, BIRT Report Documents are stand-alone entities. This really increases deployment options since you no longer need access to source data and resources for the users to view the report. Gets around firewall issues, security problems, etc… Really flexible execution.

Good luck no matter which way you go!

P.S. I did leave off one huge feature, and that is Page Level Security. If you want to develop one large report for a differentiated audience, you can do that in BIRT and rely on the tool to distribute the proper pages to the appropriate users. This can be a HUGE savings in resources (i.e. not generating 100 separate reports, but rather generating one report and slicing it up as many times as you need).

Jasper vs iBirt comparision matrix: