Jump to content
Changes to the Jaspersoft community edition download ×

  • Giulio Toffoli
    • Features: Ad Hoc Version: v5.0, v3.6.0 Product: JasperReports® Library

    When you design and preview a report in iReport Designer, iReport Designer produces a Jasper file. This file is all you need in your Java application to generate and display the report (in addition to JasperReports Library, 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 Library. 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)

    Here is the complete list of jars shipped with JasperReports Library v3.6.0 and v5.0.0 (the list is subject to change in future versions):

     v3.6.0v5.0.0
     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
    ant-1.7.1.jar
    antlr-2.7.5.jar
    barbecue-1.5-beta1.jar
    barcode4j-2.1.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
    castor-1.2.jar
    commons-beanutils-1.8.0.jar
    commons-collections-2.1.1.jar
    commons-digester-2.1.jar
    commons-javaflow-20060411.jar
    commons-logging-1.1.1.jar
    dom4j-1.6.1.jar
    groovy-all-2.0.1.jar
    hibernate3.jar
    hsqldb-1.8.0-10.jar
    iText-2.1.7.js1.jar

    jackson-annotations-2.0.5.jar
    jackson-core-2.0.5.jar
    jackson-databind-2.0.5.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.10.jar
    log4j-1.2.15.jar
    mondrian-3.1.1.12687.jar
    png-encoder-1.5.jar
    poi-3.7-20101029.jar
    poi-ooxml-3.7-20101029.jar
    poi-ooxml-schemas-3.7-20101029.jar

    rhino-1.7R3.jar
    serializer.jar
    servlet-api-2.4.jar


    spring-beans-2.5.5.jar
    spring-core-2.5.5.jar
    velocity-1.7-dep.jar
    xalan-2.7.1.jar
    xbean.jar
    xercesImpl-2.9.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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

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

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...