This example uses a jasper file to make a pdf report from data in a database. This example uses a compiled JasperReports file (with a .jasper extension) which was generated from a JasperReports definition (contained in a .jrxml file) using Jaspersoft Studio Community Edition. It also uses a DataAdapter (xml file) set up with Jaspersoft Studio as part of the report definition which points to the database name (jdbc url) and jdbc driver class, as well as the library needed for the driver.
In Jaspersoft Studio, a parameter was set up as $P{RunNumber} which is used by the primary datasource sql statement and some of the sub queries to get the data for the report. This is passed into the report generator in the example code below.
This example can be used in MIStudio/MIX and TransSECS. It requires that the database jar (jdbc driver jar) and all the Jaspersoft runtime jars be available on the runtime classpath.
The example below is set up for an MIStudio application. It uses one input parameter to the JasperReports engine, the “runid” from the MIStudio logic. The pdf report is generated to a reports directory in the MIX webserver (htmlrool/reports). The final output is the path to the report (which can be used in a URL Button, for example).
------------------------------------------------- //generate a JasperReport with one input parameter var Logger = Java.type("org.apache.log4j.Logger"); var DataSource = Java.type("com.ergotech.vib.servers.DataSource"); var FileInputStream = Java.type("java.io.FileInputStream"); var HashMap = Java.type("java.util.HashMap"); var Integer = Java.type("java.lang.Integer"); var String = Java.type("java.lang.String"); var System = Java.type("java.lang.System"); var JasperExportManager = Java.type("net.sf.jasperreports.engine.JasperExportManager"); var JasperFillManager = Java.type("net.sf.jasperreports.engine.JasperFillManager"); //Generate the report runid = runid->getIntValue(); if (runid>0) { try { parameters = new HashMap(); parameters.put("RunNumber",runid); reportStream = new FileInputStream("TheReportDefiintion.jasper"); jasperPrint = JasperFillManager.fillReport( reportStream , parameters); fileName = "/home/mix/mixruntime/htmlroot/reports/report__" + String.format("%05d", Integer.parseInt(runid)); JasperExportManager.exportReportToPdfFile(jasperPrint,fileName + ".pdf"); DataSource.logMessage("Report for run "+runid+ " generated to "+fileName+".pdf"); } catch (e) { DataSource.logMessage("error is "+e.stack); DataSource.logMessage(e); print("Error in JasperReport Generator: \n"+e.stack); print(e); } } output="/reports/report__" + String.format("%05d", Integer.parseInt(runid))+".pdf";