The features of JasperReports
JasperReports is capable of generating professional reports that include images, charts, and graphs, in addition to textual data. Some of the major JasperReports features include:
Flexible report layout
Multiple ways to present data
Multiple ways to supply data
Capability of accepting data from multiple datasources
Capability of generating watermarks
Capability of generating subreports
It is also capable of exporting reports in a variety of formats. Each of these features is briefly described in the next few sections.
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, Report Layout and Design.
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, data can be hidden or displayed in a report, or it can even be grouped into logical sections, depending on the values of the respective report fields. Let's suppose that 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 lets us control the layout of the report better. 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, Report Layout and Design.
Multiple ways to present data
JasperReports provides the ability to display report data textually or graphically using charts. JasperReports allows us to use report expressions for generating reports that display dynamic data, that is, data that is not directly passed to the report or stored anywhere; instead, it 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 through the 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.
JasperReports can generate reports using any relational database system supported by JDBC, but it is not limited to database reports. 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.table.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 that is not directly supported by JasperReports, it also allows us to create our own custom datasources. JDBC datasources are discussed in detail in Chapter 4, Creating Dynamic Reports from Databases; other datasource types, including custom datasources, are discussed in detail in Chapter 5, Working with Other Datasources.
JasperReports is capable of generating background images or text on the reports it generates. These background images can serve as watermarks for the report. Watermarks can be used for branding reports and for security purposes, as they make it difficult to forge reports. As all report pages have the same watermark, reports can maintain a consistent look and feel.
Another feature of JasperReports is its ability 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 then incorporating that separate report into a master report.
Reports generated with JasperReports can be exported to a number of formats, including PDF, Excel (XLS), and Rich Text Format (RTF). RTF is a format readable and editable by most word processors, such as Microsoft Word, OpenOffice.org Writer, StarOffice Writer, and WordPerfect. Reports created with JasperReports can also be exported to HTML, XML, CSV, plain text, and OpenDocument Format (ODF), the native file format of OpenOffice.org Writer. Exporting reports to these formats is discussed in detail in Chapter 9, Exporting to Other Formats.
The following screenshot demonstrates some of JasperReports' features, including data grouping, adding images and watermarks to a report, and exporting to PDF:
When creating this report, we took advantage of JasperReports' data grouping capabilities to create groups dividing the 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, to add a watermark and a logo in the report heading. The report font in the header section was enlarged and made bold, and the text was laid out in an easy-to-follow format.
In the previous 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.