JasperReports for Java Developers

By David R Heffelfinger
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    An Overview of JasperReports
About 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.

Publication date:
August 2006
Publisher
Packt
Pages
344
ISBN
9781904811909

 

Chapter 1. An Overview of JasperReports

This chapter presents an overview of JasperReports and explains its capabilities and features. Here is a brief outline of the topics covered in this chapter:

  • A brief history of JasperReports

  • What JasperReports is, and what it can do for us

  • A brief discussion of the JasperReports open-source license

  • The features of JasperReports

  • JasperReports' class library dependencies

  • A brief overview of the steps required to generate reports with JasperReports

  • Where to get support for JasperReports

Brief History of JasperReports

JasperReports was started by Teodor Danciu, in 2001, when he was faced with the task of evaluating reporting tools for a project he was working on. The existing solutions that he found were too expensive for his project's budget. Therefore, he decided to write his own reporting tool. The project for which he was evaluating reporting tools got canceled; but, nevertheless, he started working on JasperReports in his spare time. He registered the project on http://sourceforge.net in September, 2001. Shortly after, he started getting emails from interested potential users even though he had not yet released any code.

JasperReports version 0.1.5 was released in November, 2001. Since then, JasperReports has become immensely popular, and is currently one of the most popular (if not the most popular) Java reporting tools available. As a testament to JasperReports' enormous popularity, a Google search for java reporting tool returns the JasperReports website as its first result.

Until recently, JasperReports was basically a one-man project, with Teodor working on it in his spare time. In April 2005, a company called JasperSoft was formally launched at the MySQL User Conference in California. JasperSoft sponsors JasperReports' development, allowing Teodor and other JasperSoft developers to work full-time on JasperReports. JasperSoft also provides commercial support and services for JasperReports and related products, including the iReport Visual Designer for JasperReports. In addition to providing support for JasperReports and iReport, JasperSoft sells commercial applications incorporating JasperReports.

JasperSoft has raised over 8 million dollars in venture capital funding, no small feat in these post-dotcom days. This investment is a clear indication that venture capitalists have confidence in the success of JasperSoft, and, by extension, in the success of JasperReports. According to JasperSoft, JasperReports has been downloaded over 300,000 times, and gets over 20,000 downloads a month. It has been deployed in over 10,000 companies and Independent Software Vendors (ISVs).

 

Brief History of JasperReports


JasperReports was started by Teodor Danciu, in 2001, when he was faced with the task of evaluating reporting tools for a project he was working on. The existing solutions that he found were too expensive for his project's budget. Therefore, he decided to write his own reporting tool. The project for which he was evaluating reporting tools got canceled; but, nevertheless, he started working on JasperReports in his spare time. He registered the project on http://sourceforge.net in September, 2001. Shortly after, he started getting emails from interested potential users even though he had not yet released any code.

JasperReports version 0.1.5 was released in November, 2001. Since then, JasperReports has become immensely popular, and is currently one of the most popular (if not the most popular) Java reporting tools available. As a testament to JasperReports' enormous popularity, a Google search for java reporting tool returns the JasperReports website as its first result.

Until recently, JasperReports was basically a one-man project, with Teodor working on it in his spare time. In April 2005, a company called JasperSoft was formally launched at the MySQL User Conference in California. JasperSoft sponsors JasperReports' development, allowing Teodor and other JasperSoft developers to work full-time on JasperReports. JasperSoft also provides commercial support and services for JasperReports and related products, including the iReport Visual Designer for JasperReports. In addition to providing support for JasperReports and iReport, JasperSoft sells commercial applications incorporating JasperReports.

JasperSoft has raised over 8 million dollars in venture capital funding, no small feat in these post-dotcom days. This investment is a clear indication that venture capitalists have confidence in the success of JasperSoft, and, by extension, in the success of JasperReports. According to JasperSoft, JasperReports has been downloaded over 300,000 times, and gets over 20,000 downloads a month. It has been deployed in over 10,000 companies and Independent Software Vendors (ISVs).

 

What is JasperReports?


JasperReports is an open-source Java class library designed to aid developers with the task of adding reporting capabilities to Java applications. Since it is not a standalone tool, it cannot be installed on its own. Instead, it is embedded into Java applications by including its library in the application's CLASSPATH. JasperReports is a Java class library, and is not meant for end users, but rather is targeted towards Java developers who need to add reporting capabilities to their applications.

Although JasperReports is primarily used to add reporting capabilities to web-based applications via the Servlet API, it has absolutely no dependencies on the Servlet API or any other Java EE library. It is, therefore, by no means limited to web applications. There is nothing stopping us from creating standalone desktop or command-line Java applications to generate reports with JasperReports. After all, JasperReports is nothing but a Java class library providing an API to facilitate the ability to generate reports from any kind of Java application.

JasperReports requires a Java Development Kit (JDK) 1.3 or newer in order to successfully compile applications incorporating the JasperReports Java class library, and a Java Runtime Environment 1.3 or newer to successfully execute these applications. Older versions of JasperReports required a JDK to successfully execute JasperReports applications (strictly speaking, JasperReports required tools.jar to be in the CLASSPATH, and tools.jar is included in the JDK, not the JRE). However, from version 0.6.4, JasperReports is bundled with the Eclipse Java Development Tools (JDT) compiler, and no longer needs a JDK to execute deployed applications. Examples in this book are developed using JDK 1.5, but should compile and execute successfully with any JDK or JRE supported by JasperReports.

 

The JasperReports Open-Source License


JasperReports is licensed under the Lesser GNU Public License (LGPL). This license was chosen for JasperReports since, unlike the GPL, it allows JasperReports to be used in both open-source and closed-source applications. Applications linking to the JasperReports Java class library do not need to be open-source. However, if you consider making modifications to the existing JasperReports source code, then your modifications will have to be released under the LGPL. See http://jasperreports.sourceforge.net/license.html for the complete license.

 

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:

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.

 

Class Library Dependencies


JasperReports leverages other open-source Java libraries to implement some of their functionality. Some of the libraries JasperReports builds on include:

  • iText: iText is a PDF generation and manipulation library. It also has the ability to generate and manupulate RTF, XML, and HTML documents. JasperReports takes advantage of iText for exporting reports to PDF and RTF. More information about iText can be found at http://www.lowagie.com/iText/.

  • JFreeChart: JFreeChart is a Java library for producing various charts, including pie charts, bar charts, line and area charts, and the like. JasperReports takes advantage of JFreeChart to implement its built-in charting functionality. More information about JFreeChart can be found at http://www.jfree.org/jfreechart/index.php.

  • Jakarta POI: Jakarta POI is a Java class library to create and manipulate various Microsoft Office formats based on Microsoft's OLE 2 Compound Document format. JasperReports takes advantage of Jakarta POI for exporting reports to XLS (Microsoft Excel) format. More information about Jakarta POI can be found at http://jakarta.apache.org/poi/.

  • JAXP: JAXP refers to the Java API for parsing and transforming XML documents. It is used by JasperReports to parse XML files and is included in Java SE 5.0. It has to be downloaded separately when using earlier versions of Java SE. More information about JAXP can be found at http://java.sun.com/webservices/jaxp/index.jsp.

  • Jakarta Commons: Jakarta Commons is a collection of Java libraries providing a large number of reusable components. JasperReports takes advantage of the Digester, BeanUtils, and Logging components of Jakarta Commons to complement JAXP for XML parsing. More information about Jakarta Commons can be found at http://jakarta.apache.org/commons/.

Note

URLs are provided for informational purposes only. The JasperReports class library already includes the required JAR files listed here. There is no need for us to download them to take advantage of their functionality within JasperReports.

 

Typical Workflow


The flow chart overleaf illustrates the typical workflow while creating reports with JasperReports:

When working with JasperReports, the first step is to create a report template as an XML file. XML report templates can be hand-coded or generated by a graphical report designer. Even though JasperReports' report templates are XML files, template filenames are given an extension of .jrxml. JasperReports XML templates are commonly referred to as JRXML files, which is the term we will use for them in this book.

Here is what a typical JRXML file looks like. We will discuss JRXML files in detail in Chapter 4.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="simple_template">
<title>
<band height="50">
</band>
</title>
<pageHeader>
<band height="50">
</band>
</pageHeader>
<columnHeader>
<band height="30">
</band>
</columnHeader>
<detail>
<band height="100">
</band>
</detail>
<columnFooter>
<band height="30">
</band>
</columnFooter>
<pageFooter>
<band height="50">
</band>
</pageFooter>
<lastPageFooter>
<band height="50">
</band>
</lastPageFooter>
<summary>
<band height="50">
</band>
</summary>
</jasperReport>

The JRXML file here mostly illustrates the main elements of a JRXML file. All elements in the file are optional except for the root<jasperReport> element. The above JRXML file, when compiled and filled, will generate an empty report; not very useful in its own right, but it can be used as a template for creating more useful reports. As can be seen in the above example, each main element of the JRXML file contains a<band> element as its only child element. Bands contain the data that is displayed in the report. In the above example, all bands are empty. In real JRXML files, bands contain child elements that are used to position, format, and display the actual report data, both textual and graphical. There are commercial and open-source visual design tools that can aid in the development of JRXML files. iReport, the official JasperReports graphical report designer, is covered in detail in Chapter 10.

JRXML files are compiled into a JasperReports native binary template, either programmatically by calling the appropriate methods on the JasperReports class library (compileReportToFile()), or by using a custom ANT task (used to compile multiple XML report design files in a single operation, by specifying the root directory that contains those files or by selecting them using file patterns) provided by JasperReports. The resulting compiled template is commonly known as the Jasper file, and is typically saved to disk with a .jasper extension. The Jasper file is then used to generate the final report, by providing it with its required data. This process is known as filling the report. A JRXML file has to be compiled only once. The generated Jasper file can be filled as many times as necessary to create and display reports.

Filled reports can be saved to disk in a JasperReports native format. Reports saved in this format are known as JasperPrint files. JasperPrint file names have a .jrprint extension. JasperPrint files can only be viewed with a JasperReports-specific viewer. JasperPrint files can be exported to other formats so that they can be opened with commonly available tools like PDF viewers and word processors. Exporting to other formats is discussed in detail in Chapter 9.

 

Where to Get Help?


JasperReports has official online forums and a mailing list where questions can be asked. Both the forums and the mailing list archives can be found at the JasperReports site at http://sourceforge.net/projects/jasperreports.

The JasperReports website contains tips, tricks, JavaDoc API documentation, and a quick reference for JRXML elements. We won't repeat this information in this book, since it is readily available online. The JasperReports website can be found at http://jasperreports.sourceforge.net.

Commercial support and training is offered by JasperSoft and other third-party companies.

 

Summary


In this chapter we were introduced to JasperReports. We discussed the evolution of JasperReports from a small one-man project to a project backed and funded by a company that has raised millions of dollars in venture capital. We also had an overview of JasperReports, where we discussed that JasperReports is not a standalone reporting solution. Instead, it is a Java library that allows us to add reporting capabilities to our applications.

Next on the line was the JasperReports' open-source license (LGPL). The chapter provided us with a brief explanation of the features of JasperReports, including flexibility in report layout, the ability to display report data textually or graphically, and the ability to group report data. The JasperReports' class library dependencies were also discussed along with the typical workflow followed when designing reports. The chapter also provided us with the official online forums and mailing lists from where we can seek help.

About the Author
  • David R Heffelfinger

    David Heffelfinger is the Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area. He has been architecting, designing, and developing software professionally since 1995, and has been using Java as his primary programming language since 1996. He has worked on many large-scale projects for several clients including the US Department of Homeland Security, Freddie Mac, Fannie Mae, and the US Department of Defense. He also has a Masters degree in Software Engineering from Southern Methodist University. David is Editor-in-Chief of Ensode.net (http://www.ensode.net), a website about Java, Linux, and other technology topics.

    Browse publications by this author
JasperReports for Java Developers
Unlock this book and the full library FREE for 7 days
Start now