Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to Programmatically Create Reports In JasperReport Server


    lshannon
    • Version: v4.7

    Introduction

    A common request is how to build reports in runtime on JasperReports Server. Luckily thanks to the flexibility of JasperReports this can easily be achieved. There are some small limitations, which we will review, however for the most part this is an easy and effective technique to give you very advanced functionality in the server.

    Programatically Creating Reports

    There are two ways the report can be created when using the API. A JasperDesign Object or a JasperReport. Whats the difference?

    • JasperDesign represents the report design in a "editable" meaning there are a lot of setters available to modify the state
    • JasperReport also represents the report design, however it has been compiled and validated and is ready use with the JasperFillManager to create a JasperPrint object. The JasperReport object has less setters available making it more difficult to change its state after it has been created. This is basically the final version of the report design before you begin the process of running it against data to get an output for a client

    A Word On JasperPrint Objects

    The JasperPrint object is the created after the JasperReport Object, a JRDataSource and a map of parameters have been passed into the JasperFillManager. This is the object that can be passed to a JasperExportManager to create an output (HTML, Excel, CSV, etc). Now you might ask, why not just create the JasperPrint directly using the API.

    To see how to create a JasperPrint please review this:

    http://jasperreports.sourceforge.net/sample.reference/noreport/index.html#noreport

    The issue with creating a JasperPrint object is there is not a clean way to add this functionality to the server. So for the purposes of this acticle we will be focusing on the JasperDesign approach.

    Creating the JasperDesign Object

    The best place to start when creating reports using the api is the following JasperReports Sample:

    http://jasperreports.sourceforge.net/sample.reference/noxmldesign/index.html

    As you can see a simple report has been created. For this example what we will do is that the getJasperDesign method from the NoXmlDesignApp sample object and put it into its own class. This will give us a class where we can call a static method to get a report design object. The change we are going to make it we are going to modify the signature of the method to also take a list of parameters.

    The Flexibility of SubReports

    The nice thing about subreports is their subreport expression, this is how the report design in located, can be of type JasperReport. The expression in this case can then be a call to the Java class created in the previous step. So basically what we will have in a main report that is just a wrapper and a subreport that creates the report design on the fly.

    Values captured from the user upon report execution can be passed into the call of the helper class. The data source for the subreport can be obtained from the parent report or defined in run time.

    Deploying Everything to the Server

    Once we have it all completed it can be deployed to the server giving you a very powerful and flexible report.

    Limitations

    The big limitation here is you will be able to design the subreport to be what you want, however it is stuck within the main report. This means the main report's orientation (ie: Landscape) will be what the subreport must work within. There are potential ways to work around this, however they require customization of the server code and are beyond the scope of this article.

    Conclusion

    This technique is a great example of one of the benifits of having such a power tool as JasperReports powering JasperReports server. I hope this will be of value to you. Happy reporting!

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...