nblock's ~

Processing jasper subreports with JasperStarter

A while ago, I started to investigate JasperReports for two projects, a project at work and a private side project. The main reason, why I choose JasperReports instead of any other reporting engine out there is JasperStarter, a project that drives JasperReports from the commandline.

This blogpost describes how to process JasperReports containing subreports with JasperStarter and a XML datasource. In JAS-84 the creator of JasperStarter kindly asked for a detailed write-up and an example.

These are the tools I used for this blogpost:

If you are an Arch Linux user, you can use my AUR package for JasperStarter.

Expected result

The screenshot illustrates the expected result:

A screenshot of the rendered example report.

It is not very sophisticated, but the header line and the contact details stem from two distinct subreports that are glued together in the main report.

XML datasource

The following XML file is used as XML datasource to fill the report:

<contacts>
  <summary>
    <important>An important notice</important>
  </summary>
  <addressbook>
    <person>
      <name>ETHAN</name>
      <phone>+1 (415) 111-1111</phone>
    </person>
    <person>
      <name>CALEB</name>
      <phone>+1 (415) 222-2222</phone>
    </person>
    <person>
      <name>WILLIAM</name>
      <phone>+1 (415) 333-3333</phone>
    </person>
  </addressbook>
</contacts>

Creating the report

The report consists of three different jrxml files:

  • The main report (main.jrxml), which just includes the other two subreports.
  • The subreport for the "Page Header" band (header.jrxml). It uses the XPath expression: /contacts/summary
  • The subreport for the "Details" band: (details.jrxml). It uses the XPath expression: /contacts/addressbook/person

I won't go into details how to create the above reports with Jaspersoft Studio. There are a lot of tutorials out there, how to get started with JasperReports and Jaspersoft Studio. The important part for this blogpost is the subreport configuration in the main report.

When you include the header subreport, use the following settings:

  • Expression: header.jasper
  • Data Source Expression: ((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/contacts/summary")

For the details subreport, use:

  • Expression: details.jasper
  • Data Source Expression: ((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/contacts/addressbook/person")

Jaspersoft Studio might complain about an invalid data source expression, ignore it. I was not able to fix this error. If you have a solution to this problem, feel free to drop me a line.

Compiling the reports to jasper using JasperStarter

All .jrxml files should be compiled to .jasper files before creating the desired report format:

jasperstarter compile header.jrxml
jasperstarter compile details.jrxml
jasperstarter compile main.jrxml

Creating the final report

The .jasper files can now be used to create a report in the desired output format:

jasperstarter process -f pdf -t xml --data-file contacts.xml --xml-xpath="/" main

The main report does not use any datasource, so any XPath expression may be used above.

Download the example

The example from this blogpost is available as zip or tar.gz archive.

Credits

  • Volker Voßkämper for creating JasperStarter
  • Wolfgang Silbermayr

Update (2015-06-03)


permalink | tweet this

tagged jasperreports and jasperstarter