Skip to content

Instantly share code, notes, and snippets.

@piotrze
Created August 13, 2011 12:55
Show Gist options
  • Save piotrze/1143821 to your computer and use it in GitHub Desktop.
Save piotrze/1143821 to your computer and use it in GitHub Desktop.
Using jasper reports with play framework
package lib.reports;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
public class BaseJasperReport {
//here should be jrxml and jasper files,
//you can generate them with iReports(I'm using netbeans plugin)
// http://jasperforge.org/projects/ireport
static String REPORT_DEFINITION_PATH = "./app/reports/";
public static InputStream generateReport(String reportDefFile, Map reportParams) {
OutputStream os = new ByteArrayOutputStream();
try {
String compiledFile = REPORT_DEFINITION_PATH + reportDefFile + ".jasper";
JasperCompileManager.compileReportToFile(REPORT_DEFINITION_PATH + reportDefFile + ".jrxml", compiledFile);
JasperPrint jrprint = JasperFillManager.fillReport(compiledFile, reportParams, play.db.DB.getConnection());
JRExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jrprint);
exporter.exportReport();
} catch (Exception e) {
e.printStackTrace();
}
return new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
}
}
require:
- play
- net.sf.jasperreports -> jasperreports 4.0.1:
transitive: false
- commons-digester -> commons-digester 1.7:
transitive: false
- com.lowagie -> itext 2.1.7:
transitive: false
- net.sourceforge.jexcelapi -> jxl 2.6.10:
transitive: false
package controllers;
import play.*;
import play.mvc.*;
import java.util.Map;
import java.util.HashMap;
import models.*;
import controllers.*;
public class SomeController extends extends Base {
public static void download_report() {
Map reportParams = new HashMap();
//jrxml and jasper files should be in app/reports
renderBinary(lib.reports.BaseJasperReport.generateReport("SomeReport", reportParams), "some_report.xls");
}
}
@adilkurniaramdan
Copy link

hallo friend :D
i follow your guide to make report using jasper...
and its work very well when development process..
but i have some problem when production, when i convert project to binary using "play dist"
there is an error "java.io.FileNotFoundException: ./app/report/CustomerListing.jasper"
so where the best path to put the jasper file?

thanks

@placebocurejava
Copy link

Hi,
Could you tell me please the folder of each file 👍
1- BaseJasperReport.java : I put it in the app\models
2- dependencies.yml : in the \conf
3- SomeController.java : I have no idea !

Could you please send to us the project folder with buttons which launch .JRXML FILE etc...PLEASE, I'm new on PLAY.

Thank you in advance.

@wathitruchairam
Copy link

please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment