Wednesday, July 14, 2010

Jasper Reports with iReport Editor.


Reports Creation made simple in Java.

The report creation process has three main steps:

1. Creating a data source or a database connection used to fill the report.
2. Designing the report, including the layout of its elements and parameters to represent the data.
3. Runing the report, which includes compiling the JRXML source file in a Jasper file and filling in the data for export or onscreen display.


When you design and preview a report in iReport, iReport produces a Jasper file. This file is all you need in your Java application to generate and display the report (in addition to JasperReports, of course).

The report execution is performed by the JasperReports library, so you have to include all the required jars in your application. Which jars? If you do not have a problem shipping a lot of jars, you can include all the jars provided in JasperReports. However JasperReports includes many jars that you may not need, such as ones to create barcodes, the ones used to handle XML files (as long as you don't use an XML datasource), and ant.jar and servlet.jar, which are provided to compile the source code. Other optional jars are hsqldb.jar that is shipped to run the sample database and bsh-2.0b4.jar used to compile and run the reports using Beanshell. Other jars may be already included in your environment, for example Spring, Apache Commons Logging, and Mondrian (an OLAP database server), among others.

Here is the complete list of jars shipped with JasperReports 3.6.0 (the list is subject to change in future versions):

ant-1.7.1.jar
antlr-2.7.5.jar
barbecue-1.5-beta1.jar
barcode4j-2.0.jar
batik-anim.jar
batik-awt-util.jar
batik-bridge.jar
batik-css.jar
batik-dom.jar
batik-ext.jar
batik-gvt.jar
batik-parser.jar
batik-script.jar
batik-svg-dom.jar
batik-svggen.jar
batik-util.jar
batik-xml.jar
bcel-5.2.jar
bsh-2.0b4.jar
commons-beanutils-1.8.0.jar
commons-collections-2.1.1.jar
commons-digester-1.7.jar
commons-javaflow-20060411.jar
commons-logging-1.0.4.jar
groovy-all-1.5.5.jar
hibernate3.jar
hsqldb-1.8.0-10.jar
iText-2.1.0.jar
jasperreports-3.6.0.jar
jaxen-1.1.1.jar
jcommon-1.0.15.jar
jdt-compiler-3.1.1.jar
jfreechart-1.0.12.jar
jpa.jar
jxl-2.6.jar
log4j-1.2.15.jar
mondrian-3.1.1.12687.jar
png-encoder-1.5.jar
poi-3.2-FINAL-20081019.jar
rhino-1.7R1.jar
saaj-api-1.3.jar
servlet.jar
spring-beans-2.5.5.jar
spring-core-2.5.5.jar
xalan-2.6.0.jar
xercesImpl-2.7.0.jar
xml-apis-ext.jar
xml-apis.jar

Here is a simple application to create a PDF from a Jasper file using an empty data source. An empty data source is a data source that by default has a single record and for which you can define any field name to use in the report, but its value will always be null. It is a perfect data source to test a very simple report displaying just the label "Hello World!" For real applications, you will probably use a JDBC connection, a collection of JavaBeans, or a Hibernate session to provide data for the report. But for the aim of this tutorial, the empty data source is good enough.

import net.sf.jasperreports.engine.*; 
import net.sf.jasperreports.engine.export.*; 
import java.util.*;    
public class JasperTest {     
 public static void main(String[] args) {      
 String fileName = "test.jasper";    
 String outFileName = "test.pdf";   
 HashMap hm = new HashMap();     
    try {     
        // Fill the report using an empty data source  
           JasperPrint print = JasperFillManager.fillReport(fileName, hm, new JREmptyDataSource()); 
        // Create a PDF exporter    
         JRExporter exporter = new JRPdfExporter();
       // Configure the exporter (set output file name and print object)
          exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
          exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);       
       // Export the PDF file   
         exporter.exportReport();                     
 } catch (JRException e) {   
          e.printStackTrace();  
           System.exit(1);         
} catch (Exception e) {   
          e.printStackTrace(); 
            System.exit(1);  
       }     } }    

The most important line of code in this example is line 13. The class JasperFillManager provides several static methods to fill a Jasper file, using a data source, a connection, or even nothing. The result of the call to the method fillReport() is the special JasperPrint object which contains an in-memory representation of the report. This object can be previewed by using the JasperReports Viewer, a convenient Swing component to display the report on screen (it's the same component used by the iReport preview). As in this example, the JasperPrint object can also be passed to an exporter to generate a final document in a specific format.

On line 16, the JRPdfExporter is instantiated and later configured by setting the JasperPrint and the output file. The exportReport() method of the exporter does the rest, creating the file test.pdf.

In this sample we stored the result in a file. In other situations, such as in web applications, it is much more convenient to send the exported bytes directly to the browser. This can be done by setting the OUTPUT_STREAM parameter of the exporter to the output stream of the web application. There are many other export options, some of them common to all export formats (such as the range of pages to export), others specific of the exporter implementation.

No comments:

Post a Comment