Jasper Report Addon Tips

This document provide tips & tricks based on the experience with an community addons, jasper_report.

Multiple Selection –> 1 PDF

When Jasper is called, in JasperServer.execute(self, *args), multiple IDS can be passed, i.e., [100,101,102]. But this actually means taht these records may be selected into 1 Page (if not yet overflow to the new page).

To ensure that there will be page break with new Record ID. We need to setup Jasper Report Group as following,

Report Group

  • Right-click on the report name (in the Report Inspector)
  • Select Add field “id”
  • Provide a Group Name (e.g., UniqueID)
  • Set the Group by value to the “id” field
  • Click Finish (don’t add group header/footer)
  • On “UniqueID” group header, set “Start on new page” = True

Using Sub Report with XPath Data Source

We can use normal Adding Sub Report Wizard. But to Pass the Current Object into Sub Report, use the following guideline.

  • In Main Report,
    • Add new EVENTS_DATA_SOURCE parameter, set parameter Class = java.lang.object
  • In Sub Report widget,
    • Sub Report Expression = $P{SUBREPORT_DIR} + “ecosoft_account_invoice_subreport1.jasper”
    • Connection Type = Use a datasource expression
    • Data Source Expression = $P{EVENTS_DATA_SOURCE}

The SubReport then can be setup in similar was as in Main Report.

Sample Report: sample_reports.tar.gz


Using SQL Data Source

Using SQL as data source is not as convenient as XML, but sometime, it is needed.

Steps:

  • Create SQL Data Source as normal
  • Create an input parameter,
    • Name: IDS
    • Class: java.lang.Object
    • Default Value Expression: <n/a>
  • Create another input parameter,
    • Name: PIDS
    • Class: java.lang.List
    • Default Value Expression: $P{IDS}.toList()
  • In the SQL editor, passing IDS into SQL using the following command,
select id, name from sale_order
where $X{IN, id, PIDS}

We need to have the PIDS so that we can convert IDS into List before passing to the query. In iReport, make sure that PIDS parameter is below IDS to avoid some problem (reported by Direk).


Passing different SQL Data Source to SubReport from Main Report using XML Data Soure

If main report is using SQL Data Source, we can easily pass the connection to subreport using the same datasource by using $P{REPORT_CONNECTION}. This parameter keep the connection which is dynamically created fro the application.

However, if we are using XML Data Source on the main report, the $P{REPORT_CONNECTION} will not have connection string, and as such, we will have to create our own connection expression manually.

Steps:

  • In SubReport widget, setup as following,
    • Connection Type: Use a connection expression
    • Connection Expression:
java.sql.DriverManager.getConnection("jdbc:postgresql://localhost:5432/SQP","openerp", "openerppwd")

The reset will be similar with normal sub report, i.e., Setting up Parameters to pass into SubReport.


Grouping with reset page number

If we want to restart the page number on each group, additional setting should be done as following,

  • On the Group, set Reset Page Number = True
  • On the page count text box (the right one), set Evaluation Time = Group

Display Float as 0.0 instead of null in iReport

For some reason, by using XPath as data source, for a float field to showing 0.00 instead of blank is quite tricky. Here is how to do,

For example, given we want to pass discount value.

  • Declare new field $F{discount} from OpenERP in iReport as String
  • Declare new variable, i.e., $V{discount} as Float with expression,
$F{Discount-discount}.toString() == "" ? 0.0 : $F{Discount-discount} == null ? 0.0 : Float.parseFloat($F{Discount-discount})
  • Use the variable to display.

Parameter Reference to folder, i.e., for logo.jpg

Normally, if we reference a full path to folder, when we deploy report with logo into different path, we will face problem that the logo can’t be found on the new path.

To resolve this problem,

  • Create a new parameter SUBREPORT_DIR (can’t use other name otherwise it won’t work) and give it the folder to the report.
  • Use this SUBREPORT_DIR to reference to the logo.jpg
  • We can now deploy this report to another server without worry about path change.

Strange but works!


Order of Variables does matter

Many time you will be confused why the variable that calculated from another variable went wrong. If that happen, you might want to check the order of your variable.

For example, we have 3 variables $V{amount}, $V{total}, $V{total_string}

Where $V{amount} display in detail section, $V{total} is the sum of $V{amount} and $V{total_string} is the string representation of $V{amount}. Because each variable derieved from one another. They must be in order !!!


Amount to Word

Following is the safe way to do amount to work with jasper.

  1. Create a variable string_total with expression (Math.round($F{Total-amount_total}*100)/100).toString()
  2. Use following syntax to display amount in word
"("+(new Scriptlet()).getAmtInWords((new BigDecimal($V{string_total}==""?0:$V{string_total})).setScale(2).toString(),"THB")+")"

Example Jasper Report Files

Following are the list of good and clean example of jasper files. Useful for doing project.