CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > Create PDF using JasperReports in Java

Java Tutorial Menu

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

Create PDF using JasperReports in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: March 15, 2022 ยท 3 min read ยท 0 Comments
Share: X in ๐Ÿ”—
git change remote url

In this post, we will learn to create PDF using JasperReports in Java. 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.

Table of Contents

What is JasperReports?
JasperReports Maven Dependency
Cricketer class to hold cricketer’s data
Example to create PDF Using JasperReports in Java
Conclusion

What is JasperReports?

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.

JasperReports 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>

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;
	}
}

Example to create PDF Using JasperReports in Java

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 {
			// 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();
		}

	}
}

Conclusion

In this post, we learn to create PDF using JasperReports in Java. By following the above code and example you will be able to generate using Jasper Report template.

Related Posts:

  • Spring Data REST example.
  • JTable in Java Swing
  • Complete Guide to JaCoCo: How to Measure Java Code…
  • MySQL Commands for Developers
  • Pagination and Sorting in Spring Boot
  • How to Create Executable JAR File (3 Methods Explained)
Tags:jasper-reportjava
Was this article helpful?
โ† Previous ArticleList all services Linux
Next Article โ†’Install Angular on Windows

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap