Jasper Reports
Jasper Reports
Jasper Reports
#jasper-
reports
Table of Contents
About 1
Remarks 2
Versions 2
JasperReports library 2
Examples 4
Installation or Setup 4
JasperReports Library 4
Source code 4
Tutorials 4
Samples 5
References 5
Work flow 5
Title 5
Page Header 5
Column Header 5
Detail 6
Column Footer 6
Page Footer 6
Summary 6
Group Header 6
Group Footer 6
Background 7
No Data 7
Examples 8
With Java 11
Remarks 13
Examples 13
JasperSoft Studio 13
With Java 14
Examples 16
With Java 16
Parameters 18
Examples 18
JasperSoft Studio 18
Common Requirements 19
Chapter 6: Font-extensions 21
Examples 21
Common Issues 21
Parameters 23
Remarks 23
Examples 24
Credits 25
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: jasper-reports
It is an unofficial and free jasper-reports ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official jasper-reports.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to info@zzzprojects.com
https://riptutorial.com/ 1
Chapter 1: Getting started with jasper-reports
Remarks
There are several libraries used JasperReports Java API for creating reports with Java:
• DynamicReports
• DynamicJasper
This libraries/frameworks can build reports "on fly" with or without using report's template (jrxml
file)
Versions
JasperReports library
6.3.0 2016-06-20
6.2.0 2015-11-11
5.6.0 2014-05-27
5.5.0 2013-10-24
5.0.4 2013-03-26
5.0.0 2012-11-12
4.8.0 2012-11-05
4.7.0 2012-07-02
4.6.0 2012-05-21
4.5.0 2011-12-06
4.1.1 2011-04-18
4.0.0 2010-12-31
3.7.6 2010-10-27
3.7.5 2010-09-22
3.7.0 2009-12-08
https://riptutorial.com/ 2
Version Release date
3.6.0 2009-08-31
3.5.3 2009-07-29
3.5.0 2009-03-25
3.1.4 2009-02-10
3.1.2 2008-11-04
3.1.0 2008-09-17
3.0.1 2008-08-07
3.0.0 2008-05-19
2.0.5 2008-03-12
2.0.3 2007-12-12
2.0.0 2007-08-14
1.3.4 2007-06-11
1.3.0 2006-12-22
1.2.8 2006-11-14
1.2.0 2006-02-06
1.1.0 2005-10-21
1.0.3 2005-10-10
1.0.0 2005-07-20
0.6.8 2005-05-31
0.2.3 2002-02-06
The first version of iReport Designer was independent appliсation - iReport Classic
https://riptutorial.com/ 3
Examples
Installation or Setup
JasperReports Library
JasperReports is a open source Java based reporting tool. The JasperReports Library can be
downloaded from the Jaspersoft Community for the latest release.
In recent releases the third-party jars in the lib folder are not distributed, they need to
be download from public repositories, see distributed pom.xml for dependencies. Maven
can be used to retrieve all dependencies including the transient ones in the
target/dependence folder.
mvn dependency:copy-dependencies
• FAQ
Source code
Tutorials
• Tutorials Point
• JasperReports Ultimate Guide
https://riptutorial.com/ 4
Samples
• Sample Reference
References
• Official documentation
• Community Wiki
• Bug Tracker
Work flow
1. Design the report, create the jrxml file that defines the report layout. The jrxml can be create
by using a simple texteditor but normally an IDE (JasperSoft Studio or iReport) is used both
to speed up report development but also to have a visual view of layout.
2. Compile the report (the jrxml) to get a .jasper file or a JasperReport object. This process can
be compared with a .java file being compiled to .class.
3. Fill the report, pass parameters and a datasource to the report to generate the print object
JasperPrint that can also be saved to a .jprint file
4. View, print and/or export the JasperPrint. The most commons export format are supported
as pdf, excel, word, html, cvs etc.
Title
This band is showed once at the beginning of the report. It can be used as first page by setting the
attribute isTitleNewPage="true"
Page Header
This appears at the beginning of each page excluding first page if Title band is used and last page
if Summary band is used with setting isSummaryWithPageHeaderAndFooter="false"
Column Header
https://riptutorial.com/ 5
This appears before the detail band on each page.
Detail
This section is iterated for each record in datasource supplied. It is allowed to have multiple detail
band (detail 1, detail 2 .. detail n), the are iterated as follows
Row 1
detail 1
detail 2
detail n
Row 2
detail 1
detail 2
detail n
Column Footer
This appears below the detail band on each page where detail band is present. The default setting
is end of page (before Page footer) but this can be switch to under last detail band (last record) by
setting the attribute isFloatColumnFooter="true"
Page Footer
This appears at the bottom of each page excluding title band, summary band (without page footer)
and last non summary band if Last Page Footer is used.
Summary
This appears at the end of the report in new page if isSummaryNewPage="true" is set and with page
header and footer if isSummaryWithPageHeaderAndFooter="true"
Group Header
This section appears if a group is defined every time the group expression change, before the
detail band.
https://riptutorial.com/ 6
Group Footer
This section appears if a group is defined every time before the group expression change, after
the detail band.
Background
This band is displayed on every page as background to all other bands.
No Data
This appears only if no datasource was passed or the datasource is empty (0 records) and
whenNoDataType="NoDataSection" is set.
• .jrxml is the report design file, it's format is in human readable XML, it can be complied into
a JasperReport object and saved as a .jasper
• .jasper is the compiled version of the .jrxml and can be loaded directly into a JasperReport
object ready to be filled with data
• .jrprintis the serialized JasperPrint object, a report that have already been filled with data
and can be loaded to be printed, viewed and/or exported to desired format.
https://riptutorial.com/ 7
Chapter 2: Compile JasperReports .jrxml to
.jasper
Examples
With IDE (Integrated development environment)
In IDE Jaspersoft Studio (JSS) or the older version iReport Designer it is sufficient to press
Preview.
The JasperReports design file .jrxml will automatically be compiled to .jasper in same folder as
.jrxml if no errors are present.
https://riptutorial.com/ 8
or use the context menu "Compile Report" called from Report Inspector in iReport
https://riptutorial.com/ 9
With Apache Ant
https://riptutorial.com/ 10
xmlvalidation="true">
<classpath refid="sample-classpath"/>
<include name="**/*.jrxml"/>
</jrc>
</target>
With Java
While it is possible to compile .jrxml files into .jasper files using Java code, this incurs a
performance hit that is best avoided by pre-compiling .jrxml files using the IDE. With that in mind,
compiling .jrxml files can be accomplished using the JasperCompileManager as follows:
JasperCompileManager.compileReportToFile(
"designFile.jrxml", //Relative or absoulte path to the .jrxml file to compile
"compiled.jasper"); //Relative or absolute path to the compiled file .jasper
<build>
<plugins>
<plugin>
<groupId>com.alexnederlof</groupId>
<artifactId>jasperreports-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>jasper</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>src/main/resources/jrxml</sourceDirectory>
<outputDirectory>${project.build.directory}/jasper</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
mvn jasperreports:jasper
https://riptutorial.com/ 11
Read Compile JasperReports .jrxml to .jasper online: https://riptutorial.com/jasper-
reports/topic/4943/compile-jasperreports--jrxml-to--jasper
https://riptutorial.com/ 12
Chapter 3: Export to pdf
Remarks
To render fonts correctly in pdf font-extensions should always be used (in classpath)
Examples
With IDE (Integrated development environment)
JasperSoft Studio
In Preview, run report by clicking green arrow, if no errors the export menu will be enable, click the
export button (disk image) and select "Export As Pdf"
https://riptutorial.com/ 13
With Java
To export a you need to fill the report to get the JasperPrint object.
https://riptutorial.com/ 14
// 4. Create configuration instance
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("/path/filename.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
https://riptutorial.com/ 15
Chapter 4: Export to xls/xlsx
Examples
With Java
Map<String, Object> params = new HashMap<>(); // init map with report's parameters
params.put(JRParameter.REPORT_LOCALE, Locale.US);
params.put(JRParameter.IS_IGNORE_PAGINATION, true);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
// prepare report - passs parameters and jdbc connection
<columnHeader>
<band height="30" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="20">
<property name="net.sf.jasperreports.export.xls.auto.filter" value="Start"/>
</reportElement>
<text><![CDATA[First column with filter]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<text><![CDATA[Second column with filter]]></text>
</staticText>
<staticText>
<reportElement x="200" y="0" width="100" height="20">
<property name="net.sf.jasperreports.export.xls.auto.filter" value="End"/>
</reportElement>
<text><![CDATA[Third (Last) column with filter]]></text>
</staticText>
https://riptutorial.com/ 16
<staticText>
<reportElement x="300" y="0" width="100" height="20"/>
<text><![CDATA[Fourth column without filter]]></text>
</staticText>
</band>
</columnHeader>
The property can be set in Jaspersoft Studio with help of context menu or manually by editing
jrxml file.
https://riptutorial.com/ 17
Chapter 5: Fill report
Parameters
Parameters Column
jasperPrint The output of the fill process that can be exported to desired format
parameters The parameter Map, that if defined can be references inside report by
$P{key}
datasource A net.sf.jasperreports.engine.JRDataSource
Examples
With IDE (Integrated development environment)
JasperSoft Studio
1. If datasource or database connection is needed to fill report, create your Data Adapter in
Repository Explorer by right clicking "Data Adapters" selecting "Create Data Adapter"
2. Enter preview mode by selecting the Preview tab (no errors in deign need to be present)
3. Select desired dastasource (if no datasource is required select "One Empty Record"
https://riptutorial.com/ 18
Fill JasperReport Template using Java
Common Requirements
All reports, regardless of how the data is presented, take a path to the report template and a
parameter map. The variables are used in all examples that follow:
Using a .jrxml file incurs an extra compilation step that isn't necessary in most situations. Unless
you've written custom software to change the .jrxml before the report runs (e.g., adding or
removing columns dynamically), use the .jasper file as shown in the subsequent examples.
// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(
path, parameters, connection);
https://riptutorial.com/ 19
Using a Custom Data Source
// Populate this list of beans as per your requirements.
List<Bean> beans = new ArrayList<>();
// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(
path, parameters, datasource);
https://riptutorial.com/ 20
Chapter 6: Font-extensions
Examples
Creating and using font extensions
Create a font extension using the IDE. See the iReport or Jaspersoft Studio documentation for
details. The font extension can also be created manually.
<textElement>
<font fontName="DejaVu Sans"/>
</textElement>
To calculate font-metric (for line breaks, alignment etc) and render the font correctly, the font
needs to be mapped in the JVM (Java virtual macchine). You could install the font file directly to
the JVM but this is not encourage
We strongly encourage people to use only fonts derived from font extensions, because
this is the only way to make sure that the fonts will be available to the application when
the reports are executed at runtime. Using system fonts always brings the risk for the
reports not to work properly when deployed on a new machine that might not have
those fonts installed
DejaVu Sans
DejaVu Serif
DejaVu Sans Mono
Common Issues
Issues to consider when using font's in pdf (itext):
https://riptutorial.com/ 21
• When exporting to PDF, if the text is not rendered correctly (missing parts, characters not
showed, not wrapping or sized correctly), the font-extensions are likely missing.
• Is the actual .tff supported (OpenType) and can the font actually render the character?
Not all fonts render all characters in UTF-8.
• Is the correct encoding passed to iText? In doubts (or in general) use the encoding
Identity-H this is recommend for newer PDF standards and gives you the ability to mix
different encoding.
• Is the font embedded so that a PDF shared across computers can display the content even
if the font is not installed? If the font is not one of the 14 Standard Type 1 fonts always
embed it.
Note the version of iText used by jasper report will not render all fonts (ligaturizer problem), You
can test the ttf font and encoding directly see How can I test if my font is rendered correctly in
pdf?
https://riptutorial.com/ 22
Chapter 7: Using subreports
Parameters
Parameter Details
The pair of name and value. Not required. Several values can
returnValue
be returned from subreport to master report back
Remarks
• Subreports can be used for constructing complex reports. The reusing of existing reports is
another goal of using subreports.
• The subreport will be shown as a part of master report in case using of <subreport> element.
<subreportExpression><![CDATA["repo:subreport.jrxml"]]></subreportExpression>
<subreportExpression><![CDATA["/somePath/subreport.jasper"]]></subreportExpression>
The great explanation by @AndreasDietrich can be found at JasperServer: Unable to locate the subreport
exception post
• For some reasons the subreport can be used as a common report - without calling from the
master report (with help of <subreport> element). The subreport is always a report.
https://riptutorial.com/ 23
Examples
Passing connection to subreport; return values back to the master report
This is a snippet of master report. Two parameters and the connection (for example, jdbc) are
passing to the subreport. One value is returned from the subreport back to the master report, this
value (variable) can be used in master report
<subreport>
<reportElement x="0" y="80" width="200" height="100"/>
<subreportParameter name="someSubreportParameter">
<subreportParameterExpression><![CDATA[$P{someMasterReportParamter}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="anotherSubreportParameter">
<subreportParameterExpression><![CDATA["Some text - constant
value"]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<returnValue subreportVariable="someVariableInSubreport"
toVariable="someVariableInMasterReport"/>
<subreportExpression><![CDATA["$P{SUBREPORT_DIR} +
"subreport.jasper"]]></subreportExpression>
</subreport>
This is a snippet of master report. The datasource is passed to the subreport with help of
net.sf.jasperreports.engine.data.JRBeanCollectionDataSource constructor
<dataSourceExpression><![CDATA[net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{someField
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} +
"subreport.jasper"]]></subreportExpression>
</subreport>
https://riptutorial.com/ 24
Credits
S.
Chapters Contributors
No
Compile
2 JasperReports .jrxml Alex K, Dave Jarvis, Petter Friberg
to .jasper
https://riptutorial.com/ 25