Jasper Reports

Sachith Ariyathilaka
3 min readNov 20, 2022

--

Jasper Reports is an open-source Java reporting engine. Jasper Reports gives reporting capabilities inside the Java backend application. Jasper Reports provides multiple types of output formats, such as PDF, HTML, CSV, XML, XLS, etc. Jasper reports are responsible for generating reports based on dynamic data or static data. There are several methods to populate those data inside the reports. And also, Jasper reports provide encryption options for each and every report. Jasper Studio is the tool that has the capability to design Jasper reports.

When we integrate the Jasper report with Java Spring Boot, we have to attach the JRXML file in the resource directory as follows.

invoice.jrxml and quotation.jrxml are two types of jasper reports that produce different reports from their own. We can attach resource files, such as images, and include dynamic and static text inside the Jasper reports. First, let’s see the structure of a JRXML file.

We can design our report under the below main components. And also, we can populate dynamic and static data according to the followings.

01. Include dynamic text

We can use the following codes inside the JRXML file below the property tag.

<queryString>
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>

field name is the column name of that data that needs to be dynamic. And we can set that column name in the template, like following.

<textField isBlankWhenNull="true">
<reportElement x="30" y="-20" width="500" height="30" uuid="acef3330-01cf-4287-8ae6-7f70fc661c16"/>
<textElement>
<font size="20" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>

02. Include static text

We can include static text in the Jasper reports as follows.

<staticText>
<reportElement x="0" y="49" width="224" height="15" uuid="d706eb33-a1da-4ec2-b774-f27d6ea50b72"/>
<textElement verticalAlignment="Middle" markup="none">
<font fontName="Arial" size="8" isBold="true"/>
</textElement>
<text><![CDATA[Static text of jasper]]></text>
</staticText>

03. Include dynamic images

We can include dynamic image values as follows to the jasper report under the property tag.

<queryString>
<![CDATA[]]>
</queryString>
<field name="image" class="java.lang.String">
</field>

And images can be populated as follows in the report.

<image isUsingCache="true">
<reportElement x="240" y="21" width="71" height="51" uuid="65f086cd-ae8e-46c5-ba66-85f3a9b0bc28"/>
<imageExpression><![CDATA[$F{image}]]></imageExpression>
</image>

04. Include static images

We can include static images in the Jasper reports as follows.

<image>
<reportElement mode="Opaque" x="0" y="0" width="595" height="760" uuid="f4f76a77-2634-41d0-ba6d-1986647e8a1b"/>
<imageExpression><![CDATA["back.jpg"]]></imageExpression>
</image>

There are a lot more functions available in the Jasper reports, like dynamic parameters and encryption with static and dynamic keys. I will explain those functions also in a descriptive blog about the jasper function. And next we need to understand how there is dynamic data attached to the JRXML file and get certain types of output reports with Java.

Firstly, we need to set all the dynamic data to the JRXML file and generate a jasper print. Please refer to the following code for that.

ReportDto reportDto = getReportData(quotation);
ArrayList<ReportDto> reportData = new ArrayList<>();
reportData.add(reportDto);
InputStream input = null;
input = Objects.requireNonNull(this.getClass().getClassLoader().
getResource("quotation.jrxml")).openStream();
JasperDesign design = null;
design = JRXmlLoader.load(input);
JasperReport jasperReport = JasperCompileManager.compileReport(design);
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(reportData);
Map<String, Object> parameters = new HashMap<>();
parameters.put("createdBy", "SachithAriyathilaka");
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, dataSource);

And next we need to export our jasper print into the desired format. Following codes describe how to export into PDF format.

OutputStream out = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(print, out);

According to the following instruction, we can easily design and implement Jasper reports with dynamic reporting.

--

--

Sachith Ariyathilaka
Sachith Ariyathilaka

No responses yet