Generate PDF Report using JASPER Report API in JAVA

 

Hello friends! Today I will show you how to generate a PDF report using JASPER Report API. I am going to use iReport tool here to generate the xml file (.jrxml). This xml file is used by JASPER API to generate the PDF report. This tool provides a graphical interface which makes work simple. The screenshots to generate a simple report is given below.

Required Resources:
1)iReport tool from jasper soft
2)jaspersoft jars. (Once you’ve installed the iReport tool all the required jars can be found in “C:\Program Files (x86)\Jaspersoft\iReport-5.6.0\ireport\modules\ext\”. Below are JARs required to run the sample application I have provided in this tutorial.
a.commons-beanutils-x.x.x.jar
b.commons-collections-x.x.x.jar
c.commons-digester-x.x.jar
d.commons-logging-x.x.jar
e.groovy-all-x.x.x.jar(if the report language is deafult Groovy)
f.iText-x.x.x.jar(For PDF Exports)
g.jasperreports-x.x.x.jar

Steps to generate jrxm using iReport:
1)Step 1:
Open iReport and click on File menu and click on new.
Step 1

2)Step 2:
Then select blank A4 template and click on ‘Open this template’.
Step 2

3)Step 3:
Then give name, location and click on next. The name given here will be the name of the xml, which we need to pass to the report generator.
Step 3

4)Step 4:
Then click on finish to generate the blank template.
Step 4

5)Step 5:
Here I am generating a student report where Roll No and Name are exported in PDF file. So, now we have to add two fields in the blank report. To do this, right click on fields in “Report Inspector” window and click on add field. Similarly add another field.
Step 5

6)Step 6:
Now rename the added fields as shown below. The name of the fields should be same as the properties available in “StudentVo.java class”. If different names given then the mapping can’t be done and hence report will not be generated.
Step 5

7)Step 7:
Now drag and drop those fields in “Detail 1” band. By doing this column names will be automatically added to the “Column Header” band. After this by dragging fields fix the alignments.
Step 6

8)Step 8:
Then select all the headers and fields right click on them. Then click on “Padding and Boarders” where you can apply boarders.
Step 7

9)Step 9:
Then save and compile the report. If everything is fine report will be compiled successfully.

Folder Structure:
Folder Structure

Files:
1)StudentVo.java

This is the class which contains the student details to be exported to the report.

public class StudentVo{
	private String rollNo;
	private String name;

	public StudentVo(String rollNo, String name){
		this.rollNo = rollNo;
		this.name = name;
	}

	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

2)ReportGenerator.java
This is the class where the code to generate the pdf is written. generateJasperReportPDF() takes # arguments, 1st one is the String which is the name of the ‘.jrxml’ file generated using iReport. 2nd is the output stream, and the 3rd one is the List (here we are passing the list of StudentVo). Please go through the code to understand better.

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
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;
import net.sf.jasperreports.engine.export.JRPdfExporter;


public class ReportGenerator {
	public static void main(String[] args) throws Exception {
		List<StudentVo> list = new ArrayList<StudentVo>();
		list.add(new StudentVo("1","ashish"));
		list.add(new StudentVo("2","deepak"));
		list.add(new StudentVo("3","rabi"));
		list.add(new StudentVo("4","anil"));

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

		FileOutputStream fos = null;
		try {
			byte[] bytes=new ReportGenerator().generateJasperReportPDF("studentReport", outputStream, list);
			if(bytes.length>1){
				File someFile = new File("D:/Temp/testReport.pdf");
				fos = new FileOutputStream(someFile);
				fos.write(bytes);
				fos.flush();
				fos.close();
				System.out.println("<<<<<<<<<<<<Report Created>>>>>>>>");
			}
		}catch(FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public byte[]  generateJasperReportPDF(String jasperReportName, ByteArrayOutputStream outputStream, List reportList) {
		JRPdfExporter exporter = new JRPdfExporter();
		try {
			String reportLocation = "D://Temp//"+jasperReportName+".jrxml";
			JasperReport jasperReport = JasperCompileManager.compileReport(reportLocation);
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null, new JRBeanCollectionDataSource(reportList));
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);	
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
			exporter.exportReport();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error in generate Report..."+e);
		} finally {
		}
		return outputStream.toByteArray();
	}
}

Leave a comment

SUBSCRIBE TO NEWSLETTER

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Categories