Create PDF using Jasper Report Java

In this post, we will learn to create PDF using JasperReport. Let’s take a simple example.

We have a list of cricketers’ data and want to add these data in our PDF file. Let’s assume that we already have a jrxml file created. To create a jrxml file we can use JasperReport Studio and it can be downloaded from here.

In this post, we don’t focus to create a jrxml file. We focus on generating PDF using the jrxml file.

1. What is JasperReport

Wikipedia Definition

JasperReports is an open source Java reporting tool that can write to a variety of targets, such as: screen, a printer, into PDF, HTML, Microsoft Excel, RTF, ODT, comma-separated values (CSV) or 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.

2. Maven Dependency


3. Cricketer class to hold cricketer’s data

public class Cricketer {
	private String id;
	private String name;
	private String address;

	public Cricketer() {

	public Cricketer(String id, String name, String address) {
		super(); = id; = name;
		this.address = address;

	public String getId() {
		return id;
	public void setId(String id) { = id;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getAddress() {
		return address;
	public void setAddress(String address) {
		this.address = address;

3. PDFGenerator class that generates PDF file

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

public class PdfGenerator {

	private static final String CRICKETER_REPORT_JRXML = "cricketerReport.jrxml";

	public static void main(String[] args) {

		List<Cricketer> cricketers = Arrays.asList(new Cricketer("C1", "Radha Krishnan", "Kathmandu"),
				new Cricketer("C2", "Sachine Tendulkar", "Mumbai"), new Cricketer("C3", "Yuvaraj Singh", "Punjab"),
				new Cricketer("C4", "Virat Kohli", "Delhi"));

		new PdfGenerator().generate(cricketers);

	private void generate(List<Cricketer> cricketers) {
		String fileName = "Cricketers.pdf";
		try {
//			file = new File(fileName);
			// Fetching the .jrxml file from the resources folder.
			final InputStream stream = this.getClass().getResourceAsStream(CRICKETER_REPORT_JRXML);
			// Compile the Jasper report from .jrxml to .japser
			final JasperReport report = JasperCompileManager.compileReport(stream);
			// Fetching the employees from the data source.
			final JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(cricketers);
			// Adding the additional parameters to the pdf.
			final Map<String, Object> parameters = new HashMap<>();
			 * Filling the report with the data and additional parameters information.
			final JasperPrint print = JasperFillManager.fillReport(report, parameters, source);
			JasperExportManager.exportReportToPdfFile(print, fileName);
		} catch (JRException e) {


4. Conclusion

In this post, we learn to generate a PDF file using JasperReport.

Leave a Comment