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…. 🙂