The JasperReports Ultimate Guide
The JasperReports Ultimate Guide
Version 1.0
Author:
Teodor Danciu
Copyright © 2002 Teodor Danciu (teodord@hotmail.com). All rights reserved.
Page 1
The JasperReports Ultimate Guide
Table of contents
4.1
4.2
4.3
5.1
5.2
Introduction______________________________________________________4
1
2 API Overview_____________________________________________________5
3 Main Tasks and Processes _________________________________________10
XML Parsing _____________________________________________________ 10
3.1
Compiling Report Designs __________________________________________ 10
3.2
Report Design Preview _____________________________________________ 12
3.3
Filling Reports ____________________________________________________ 13
3.4
Viewing Reports___________________________________________________ 15
3.5
Printing Reports___________________________________________________ 16
3.6
3.7
Exporting Reports _________________________________________________ 17
3.8 Object Loading and Saving__________________________________________ 17
4 Report Designs___________________________________________________19
DTD Reference____________________________________________________ 19
XML Encoding____________________________________________________ 20
Report Properties__________________________________________________ 21
5 Report Data _____________________________________________________25
Expressions_______________________________________________________ 25
Parameters _______________________________________________________ 26
Built-in Report Parameters ______________________________________________28
Data Source_______________________________________________________ 29
Report Query _____________________________________________________ 31
Fields ____________________________________________________________ 32
Variables_________________________________________________________ 34
Calculations __________________________________________________________35
Built-in Report Variables________________________________________________37
6 Report Sections __________________________________________________38
6.1 Main Report Sections ______________________________________________ 39
Data Grouping ____________________________________________________ 40
6.2
7
Scriptlets________________________________________________________43
8 Report Elements _________________________________________________44
Text Elements_____________________________________________________ 48
Fonts and Unicode Support ______________________________________________49
Static Texts __________________________________________________________53
Text Fields ___________________________________________________________53
8.2 Graphic Elements__________________________________________________ 56
Lines _______________________________________________________________57
Rectangles ___________________________________________________________58
Images ______________________________________________________________58
5.3
5.4
5.5
5.6
8.2.1
8.2.2
8.2.3
8.1
8.1.1
8.1.2
8.1.3
5.2.1
5.6.1
5.6.2
Page 2
The JasperReports Ultimate Guide
9
10
8.2.4
9.1
9.2
Charts and Graphics____________________________________________________60
8.3 Hyperlinks _______________________________________________________ 60
Element Groups ___________________________________________________ 62
8.4
Subreports ______________________________________________________63
Subreport Parameters ______________________________________________ 65
Subreport Data Source _____________________________________________ 66
Advanced JasperReports _________________________________________67
10.1 XML Report Designs Loading and Writing ____________________________ 67
10.2 Implementing Data Sources _________________________________________ 67
10.3 Customizing Viewers _______________________________________________ 68
10.4 Exporting to New Output Formats____________________________________ 68
Page 3
The JasperReports Ultimate Guide
1
Introduction
The JasperReports library is a very powerful and flexible report-generating tool that has the ability to
deliver rich content onto the screen, to the printer or into PDF, HTML or XML files. Hopefully, in the
future, other output formats such as CSV, XLS, RTF and other will be supported.
The library is entirely written in Java and can be used in a variety of Java enabled applications,
including J2EE or Web applications, to generate dynamic content. Its main purpose is to help creating
page oriented, ready to print documents in a simple and flexible manner.
JasperReports organizes data according to the report design defined in an XML file. This data may
come from various data sources including relational databases, collections or arrays of Java objects.
Users can plug the reporting library to custom made data sources, by implementing a simple interface,
as you will see later in this book.
In order to fill a report with data, the XML report design must be compiled first. Through compilation,
a report design object is generated and then serialized in order to store it on disk or send it over the
network. This serialized object is then used when the application wants to fill the specified report
design with data. In fact, the compilation of a report design implies the compilation of all Java
expressions defined in the XML file representing the report design. Various verifications are made at
compilation time, to check the report design consistency. The result is a ready to fill report design that
will be then used to generate documents on different sets of data.
To fill a report design, the engine needs to receive the report data. This may come in various forms.
Some of this data can be passed in as report parameters. But most of the data will be found in the report
data source. The reporting engine can directly receive special data source objects from which to get the
information to put on the report, or can deal itself with a supplied JDBC connection object, if that data
is located in a relational database.
The result of the report filling operation is a new object that represents the ready to print document.
This one is also serialized for storage on disk or network transfer. It can be viewed directly using the
JasperReports built-in viewer or can be exported to other, more popular formats like PDF, HTML or
XML.
Page 4
The JasperReports Ultimate Guide
2 API Overview
Most of the time, when using the JasperReports library, people will get to work only with a few classes
and won't have to get to know the entire API, in order to benefit from all its features and functionality.
In this section we shall get a close look at the classes and interfaces which are significant when using
the library and see how to make use of them in applications that need the reporting functionality that
JasperReports offer.
JasperFillManager
parse
XML
JasperDesign
compile
JasperReport
JasperPrint
fill
JRXmlLoader
JRCompiler
JasperPrintManager
print
export
Screen
Printer
PDF
HTML
XML
JasperCompileManager
JasperExportManager
- figure 1 -
Class dori.jasper.engine.design.JasperDesign
We begin with this class because instances of it represent the raw material, which the JasperReports
library uses for report generating purposes. Such instances are obtained after the XML report design
files are parsed by the library internal XML parsing procedures, for example, but can be build
programmatically, by the application that uses JasperReports, if working with XML files is not an
option. Among the supplied samples that come with the project source files, there is one called
noxmldesign
a
dori.jasper.engine.design.JasperDesign object without editing an XML report design file.
dynamically
create
check
how
that
you
can
see
to
to
All instances of the dori.jasper.engine.design.JasperDesign class are subject to compilation
before being used for filling and report generation. This is why they are considered the raw material for
the library.
Class dori.jasper.engine.JasperReport
Instances of this class represent compiled report design objects. These can be obtained only as a result
of the JasperReports report compilation process and are ready to use for filling with data and report
generation.
Through compilation, along with various consistency checks and rearrangements of the report elements
for more rapid later utilization, the library creates a temporary class file containing all the report
expressions such as report variables expressions, text field and image expressions, group expressions,
etc.
This temporary Java source file is compiled on the fly using either the Java compiler classes from the
JDK used to run the application. If the tools.jar file is not found in the classpath in order to do that,
the compilation will go ahead anyway by launching at runtime the javac.exe compiler, behind the
scenes. The
resulting
dori.jasper.engine.JasperReport for using when filling the report with data, to evaluate the
various report expressions at runtime.
bytecodes
resulting
stored
class
file
the
are
the
of
in
Page 5
Class dori.jasper.engine.JasperCompileManager
The JasperReports Ultimate Guide
This is the class that exposes all the library functionality concerning the report compilation.
It has various methods that allow the users to compile XML report designs found in files on disk or that
come from input streams. It also lets you compile in-memory report designs by directly passing a
dori.jasper.engine.design.JasperDesign
corresponding
dori.jasper.engine.JasperReport object.
receiving
the
object
and
Other utility methods include report design verification and XML report design generation for in-
memory constructed dori.jasper.engine.design.JasperDesign class instances. Those are very
useful especially in GUI tools that simplify the report design work.
Class dori.jasper.engine.JasperPrint
After a compiled report design is filled with data, the resulting document comes in the form of a
dori.jasper.engine.JasperPrint instance. Such an object can be viewed directly using the
JasperReports build-in report viewer, or can be serialized for disk storage and later use, or for sending
it over the network.
The instances of this class represent the output of the report filling process of the JasperReports library
and can be considered as a custom format for storing full featured, page oriented documents. They can
be transformed into other more popular formats like PDF, HTML, XML or other by using the library's
export functionality.
Interface dori.jasper.engine.JRDataSource
JasperReports is a very flexible reporting tool as far as the source of the report data is concerned. It lets
people use any kind of data source they might want to, as long as they can provide an appropriate
implementation of this interface, so that the reporting engine can interpret and retrieve the data from
that data source when filling the reports.
Normally, every time a report is being filled, an instance of this interface is always supplied or created
behind the scenes by the reporting engine.
Class dori.jasper.engine.JRResultSetDataSource
This is a default implementation of the dori.jasper.engine.JRDataSource interface.
Since most of the reports are generated using data that comes from a relational database, JasperReports
includes by default this implementation that wraps a java.sql.ResultSet object.
This class can be instantiated on purpose, to wrap already loaded result sets, before passing it to the
report filling routines, but it is also used by the reporting engine to wrap the data retrieved from the
database after having executed through JDBC the report query, if present.
Class dori.jasper.engine.data.JRTableModelDataSource
This class represents another default implementation of the dori.jasper.engine.JRDataSource
interface that is shipped with the library. It wraps a javax.swing.table.TableModel object and
can be used in Java Swing applications to generate reports using data that has already been loaded into
on-screen tables.
Page 6
The JasperReports Ultimate Guide
Class dori.jasper.engine.JREmptyDataSource
Being the most simple implementation of the dori.jasper.engine.JRDataSource interface, this
class can be used in reports that do not display data from the supplied data source, but rather from
parameters, and when only the number of virtual rows in the data source is important.
Many of the provided samples such as fonts, images, shapes and unicode use an instance of this class
when filling the reports, to simulate a data source with one record in it, but with all the fields null.
Class dori.jasper.engine.JasperFillManager
This class is the façade to the report filling functionality of the JasperReports library.
It exposes a variety of methods that receive a report design in the form of an object, file or input stream
and produce a document also in various output forms: object, file or output stream.
But along with the report design, the report filling engine has to receive also the data source from
which to retrieve data and the values for the report parameters, in order to generate the documents.
Parameter values are always supplied in a java.util.Map object in which the keys are the report
parameter names.
The data source can be supplied in two different forms, depending on the situation:
Normally, it has to be is supplied as a dori.jasper.engine.JRDataSource object, like already
mentioned above.
But with the majority of reports in the reporting world being filled with data from relational databases,
JasperReports has a built-in default behavior that lets people specify an SQL query in the report design
itself. This SQL query is executed in order to retrieve the data to use when filling the report at runtime.
In such cases, the only thing JasperReports needs is a java.sqlConnection object, instead of the
usual data source object. It needs this connection object to connect to the relational database
management system through JDBC and execute the report query.
It will automatically creates a dori.jasper.engine.JRResultSetDataSource behind the scenes
to wrap the java.sql.ResultSet object returned after the execution of the query and passes it to the
normal report filling process.
Class dori.jasper.engine.JRAbstractScriptlet
Scriptlets are a very powerful feature of the JasperReports library. They allow users to write custom
code that will be executed by the reporting engine during the report filling process. This user code can
deal with report data manipulation and gets executed at well-defined moments such as page, column or
group breaks, opening a whole new range of possibilities in customizing the content of the generated
documents.
Class dori.jasper.engine.JRDefaultScriptlet
This is a convenience subclass of the dori.jasper.engine.JRAbstractScriptlet class. Most of
the time users will chose to subclass this when working with scriptlets, so they won't have to implement
all the abstract methods declared in the abstract class.
Class dori.jasper.engine.JasperPrintManager
We are talking here about a Java reporting tool and what reporting tools are meant for is printing.
After having filed a report, we have the option of viewing it, exporting it into a different format and last
but not least printing it.
Page 7
The JasperReports Ultimate Guide
In JasperReports, we can print reports using this particular manager class, which is a façade to the
printing functionality exposed by the library.
We can find here various methods that send to the printer entire documents or only portions of it, either
by displaying the print dialog or not.
The content of a page from a JasperReports document can be displayed by generating a
java.awt.Image object for it using this manager class.
Class dori.jasper.engine.JasperExportManager
As already mentioned, JasperReports allows transforming generated documents from its proprietary
format into more popular documents formats such as PDF, HTML or XML. With time, this part of the
JasperReports functionality will be extended to support other formats like CSV, XSL and other.
This manager class has various methods that ca process data that comes from different sources and
goes to different destinations: files, input and output streams, etc.
Class dori.jasper.engine.JasperRunManager
Sometimes is useful to produce documents only in a popular format such as PDF or HTML, without
having to store on disk the serialized, intermediate dori.jasper.egine.JasperPrint object,
produced by the report filling process.
This can be achieved using this manager class which immediately exports the document produced by
the report filling process into the desired output format.
The use of this manager class is shown and can be tested in the supplied webapp sample, where PDF
and HTML content is produced on the fly.
Class dori.jasper.view.JRViewer
This class is different from the rest of the classes listed above in the way that it is more like pluggable
visual component than an utility class.
It can be used in Swing based applications to view the reports generated by the JasperReports library.
- figure 2 -
This visual component is not meant to satisfy everybody. It was included in the main library more like
a demo component, to show how the core printing functionality can be used to display the reports in
Swing based applications, by generating java.awt.Image objects for the document pages, using the
dori.jasper.engine.JasperPrintManager class.
Page 8