Home Data JasperReports 3.5 for Java Developers

JasperReports 3.5 for Java Developers

books-svg-icon Book
eBook $28.99 $19.99
Print $48.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $28.99 $19.99
Print $48.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    An Overview of JasperReports
About 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.
Publication date:
August 2009
Publisher
Packt
Pages
368
ISBN
9781847198082

 

Chapter 1. An Overview of JasperReports

In this chapter, along with an overview of JasperReports, we explain its capabilities and features. Here is a brief outline of the topics we will cover in this chapter:

  • A brief history of JasperReports

  • What JasperReports is and what it can do for us

  • The JasperReports open source license

  • The features of JasperReports

  • JasperReports' class library dependencies

  • The steps required to generate reports with JasperReports

  • Where to get support for JasperReports

 

A brief history of JasperReports


JasperReports was started in 2001 by Teodor Danciu when he was faced with the task of evaluating reporting tools for a project he was working on. The existing solutions, he found, were too expensive for his project's budget; therefore, he decided to write his own reporting engine. The project for which he was evaluating reporting tools got canceled, but nevertheless, he started to work on JasperReports in his spare time. He registered the project on 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; it is currently one of the most popular Java reporting tools available.

At first, JasperReports was basically a one-man project, with Teodor working on it in his spare time. Then, in April 2005, a company called JasperSoft was formally launched at the MySQL User Conference in California. JasperSoft now 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 dot-com 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.

 

What exactly is JasperReports?


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

JasperReports is licensed under the Lesser GNU Public Library (LGPL). This license was chosen for JasperReports because, 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 are considering making modifications to the existing JasperReports source code, then your modifications will have to be released under the LGPL. Refer to http://www.gnu.org/copyleft/lesser.html for the complete license.

Although JasperReports is primarily used to add reporting capabilities to web-based applications using 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 that stops 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.4 or newer in order to successfully compile applications incorporating the JasperReports Java class library and a Java Runtime Environment (JRE) 1.3 or newer to successfully execute these applications. The older versions of JasperReports required a JDK to successfully execute JasperReports applications (strictly speaking, JasperReports requires tools.jar to be in the CLASSPATH, and tools.jar is included in the JDK, not JRE). As of version 0.6.4, however, 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 were developed using JDK 1.6 but should compile and execute successfully with any JDK or JRE supported by JasperReports.

 

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.

 

Class library dependencies


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

  • iText: A PDF generation and manipulation library. It has the ability of generating and manipulating RTF, XML, and HTML documents. JasperReports takes advantage of iText in order to export reports to PDF and RTF. More information about iText can be found at http://www.lowagie.com/iText/.

  • JFreeChart: A Java library for producing various charts, including pie charts, bar charts, line charts, and area charts. 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.

  • Apache POI: A Java class library to create and manipulate various Microsoft Office formats, such as Microsoft's OLE 2 Compound Document format. JasperReports takes advantage of POI to export reports to XLS (Microsoft Excel) format. More information about Apache POI can be found at http://poi.apache.org/.

  • JAXP: Java API for parsing and transforming XML documents. JAXP is used by JasperReports to parse XML files. JAXP is included with Java SE 5.0 and it can be downloaded separately when using earlier versions of Java SE. More information about JAXP can be found at https://jaxp.dev.java.net/.

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

    Note

    URLs provided here are 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 following flow chart illustrates the typical workflow followed when 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 handcoded or generated by a graphical report designer. Even though JasperReports report templates are XML files, by convention 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 in this book.

Let's take a look at what a typical JRXML file looks like:

<?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>

This JRXML file illustrates the main elements of a JRXML file. All elements in the JRXML file are optional except for the root <jasperReport> element. This JRXML file, when compiled and filled, will generate an empty report, not very practical in its own right, but it can be used as a template for creating more useful templates. As can be seen in the 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 example, all the bands are empty. In real JRXML files, bands contain child elements 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. One such tool, iReport, the official JasperReports graphical report designer, is covered in detail in Chapter 10, Graphical Report Design with iReport.

JRXML files are "compiled" into a JasperReports native binary template either programmatically by calling the appropriate methods on the JasperReports class library, or by using a custom ANT task 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, while the generated jasper file can be filled as many times as necessary to create and display reports.

The filled reports can be saved to disk in a JasperReports' native format. Reports saved in this format are known as JasperPrint files. The JasperPrint file names have a .jrprint extension. JasperPrint files can only be viewed with a JasperReports specific viewer. The 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, Exporting to Other Formats.

 

Where to get help


JasperForge, the official JasperSoft online portal, is the best place to get help with JasperReports and other JasperSoft products. JasperForge can be found at http://jasperforge.org.

JasperForge has official online forums where questions about their products, including JasperReports, can be asked. The JasperReports online forums can be found at http://jasperforge.org/plugins/espforum/browse.php?group_id=102&forumid=103.

The JasperReports page at JasperForge contains tips, tricks, JavaDoc API documentation, and a quick reference for JRXML elements. We won't repeat that information in this book, as it is readily available online. Commercial support and the 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 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. Finally, this chapter provided us with the official online forums where we can seek help.

JasperReports 3.5 for Java Developers
Unlock this book and the full library FREE for 7 days
Start now