Book Image

JasperReports for Java Developers

Book Image

JasperReports for Java Developers

Overview of this book

JasperReports is the world's most popular embeddable Java open source reporting library, providing Java developers with the power to easily create rich print and web reports. This book shows you exactly how to get started, and develop the skills to get the most from JasperReports. The book steers you through each point of report setup, to creating, designing, formatting, and exporting reports with data from a wide range of datasources, and integrating JasperReports with other Java frameworks. Starting with the basics of adding reporting capabilities to your application, and creating report templates you will first see how to produce your reports through the use of JRXML files, custom ANT targets, and then see preview them in both the browser and the native browser of JasperReports. Getting data into your reports is the next step, and you will see how to get data from a range of datasources, not only databases, but XML files, and Java Objects, among others. You will create better looking reports with formatting and grouping, as well as adding graphical elements to the report. You will export your reports to a range of different formats, including PDF and XML. Creating reports will be made even easier with a walkthrough of the iReport Designer visual designing tool. To round things off, you will see how to integrate your reports with other Java frameworks, using Spring or Hibernate to get data for the report, and Java Server Faces or Struts for presenting the report.
Table of Contents (13 chapters)
12
Index

Features of JasperReports

In addition to textual data, JasperReports is capable of generating professional reports including images, charts, and graphs. Some of the major JasperReports features include:

  • It has flexible report layout.
  • It is capable of presenting data textually or graphically.
  • It allows developers to supply data in multiple ways.
  • It can accept data from multiple datasources.
  • It can generate watermarks.
  • It can generate subreports.
  • It is capable of exporting reports to a variety of formats.

Each of these features is briefly described in the next few sections.

Flexible Report Layout

JasperReports allows us to separate data into optional report sections. These sections include:

  • The report title, which will appear once at the top of the report.
  • A page header, which will appear at the top of every page.
  • A detail section, which typically contains the primary report data.
  • A page footer, which will appear at the bottom of every page.
  • A summary section, which will appear at the end of the report.

All of these and other report sections are discussed in detail in Chapter 6. In addition to allowing us to define report sections, JasperReports allows the creation of elaborate dynamic layouts based on the contents of the report. For example, depending on the value of a report field, data can be hidden or displayed in a report, or data can be grouped into logical sections. Say, we are creating a report about cars. JasperReports allows us to group the data by make, model, year, or a combination of these or any other piece of data displayed on the report. Data grouping allows us to better control the layout of the report. Data-group definitions can also be used to calculate subtotal values based on a subset of the report data. Groups are also used to define datasets for charts and graphs. Data grouping is discussed in detail in Chapter 6.

Multiple Ways to Present Data

JasperReports provides the ability to display report data textually or graphically via charts. JasperReports allows us to use report expressions to generate reports that display dynamic data. That is, data that is not directly passed to the report or stored anywhere, but is calculated from the data contained in the datasource and/or report parameters.

Multiple Ways to Supply Data

JasperReports allows developers to pass data to a report by passing it report parameters. Report parameters can be instances of any Java class.

Data can also be passed to a report by using special classes called datasources. Report parameters and datasources can be combined for maximum flexibility.

Multiple Datasources

JasperReports can generate reports using any relational database system supported by JDBC. However, it is not limited to database reports only. It can generate reports from a number of datasources, including XML files, Plain Old Java Objects (POJOs), any class implementing the java.util.Map interface, and any class implementing the javax.swing.TableModel interface.

JasperReports also supports empty datasources, which are used for simple reports that have no dynamic data displayed. If we need to create a report from a datasource, not directly supported by JasperReports, it allows us to create our own custom datasources. JDBC datasources are discussed in detail in Chapter 4. Other datasource types, including custom datasources, are discussed in detail in Chapter 5.

Watermarks

JasperReports is capable of generating background images or text on the reports it generates. These background images can serve as a sort of 'watermark' for the report. A watermark is like a secondary image that is laid over the primary image.

Watermarks can be used for branding reports and for security purposes, since they make it difficult to forge reports. All report pages have the same watermark, which gives them a consistent look and feel.

Subreports

Another feature of JasperReports is that it allows us to create subreports, or reports within reports. Subreports simplify report design significantly by allowing us to extract complex report sections into a separate report, and incorporating that separate report into a master report.

Exporting Capabilities

Reports generated with JasperReports can be exported to a number of formats, including PDF (Portable Document Format), XLS (Excel), RTF (Rich Text Format, a format readable and editable by most word processors, including, but certainly not limited to, Microsoft Word, OpenOffice.org Writer, StarOffice Writer, and WordPerfect), HTML (HyperText Markup Language), XML (Extensible Markup Language), CSV (Comma-separated Values), and plain text. Exporting reports to these formats is discussed in detail in Chapter 9. There is also a third-party library to export JasperReports' reports to the OpenDocument Format (ODF). The OpenDocument Format is a standard XML-based file format specification for office applications developed by the Organization for the Advancement of Structured Information Standards (OASIS). OpenOffice.org version 2.0 uses ODF as its default format.

Note

The JasperReports OpenDocument Format exporter was developed in the summer of 2005, as a part of Google's Summer of Code program. More information about the JasperReports OpenDocument Format exporter can be found at http://netmoc.cpe.ucf.edu/Projects/jasper.html.

The screenshot overleaf demonstrates some of the features of JasperReports, including data grouping, adding images and watermarks to a report, and exporting to a PDF:

Exporting Capabilities

When creating the report given in the screenshot, we took advantage of the data-grouping capabilities of JasperReports to group data by country, state, and city. This grouping allowed us to display the data in a logical, easy-to-follow way. We also took advantage of JasperReports' ability to display images and added a watermark and a logo in the report heading. The report font was modified in the header section to display it in a bigger font and in bold. Text was laid out in an easy-to-follow format.

In the screenshot, the freely available Evince document viewer was used to display the PDF report. Of course, reports exported to PDF can be viewed with any PDF viewer, including Adobe Acrobat, Foxit, and xPDF.