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

<dependency>
	<groupId>net.sf.jasperreports</groupId>
	<artifactId>jasperreports</artifactId>
	<version>6.17.0</version>
</dependency>
<dependency>
	<groupId>net.sf.jasperreports</groupId>
	<artifactId>jasperreports-fonts</artifactId>
	<version>6.17.0</version>
</dependency>

3. Cricketer class to hold cricketer’s data

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

	public Cricketer() {
		super();
	}

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

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

3. PDFGenerator class that generates PDF file

import java.io.InputStream;
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;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

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) {
			e.printStackTrace();
		}

	}
}

4. Conclusion

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

Leave a Comment