Book Image

JasperReports 3.5 for Java Developers

Book Image

JasperReports 3.5 for Java Developers

Overview of this book

Do you want to create easily understood, professional, and powerful reports from disordered, scattered data using a free, open source Java class library? If your answer is yes, this book on JasperReports is what you are looking for. 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. The book has been fully updated to use JasperReports 3.5, the latest version of JasperReports. Previously accepted techniques that have now been deprecated have been replaced with their modern counterparts. All examples in the book have been updated to use XML schemas for report templates. Coverage of new data sources that JasperReports now supports has been added to the book. Additionally, JasperReports can now export reports to even more formats than before and exporting reports to these new formats is covered in this new edition of the book. This book shows you exactly how to get started, and develop the skills to get the most from JasperReports. It introduces you to the latest version of JasperReports, and gets you creating complex and elegant reports. The book steers you through each point of report setup, to creating, designing, formatting, and exporting reports with data from a wide range of data sources, 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 preview them in both the web 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 data sources, 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 your reports. 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. All examples have been updated to use XML schemas. New export formats, such as OpenDocument Text, and new data sources now supported by JasperReports are now covered in this updated edition.
Table of Contents (17 chapters)
JasperReports 3.5 for Java Developers
Credits
About the Author
About the Reviewers
Preface
Index

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.

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, 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.

Multiple datasources

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.

Watermarks

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.

Subreports

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.

Exporting capabilities

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.