Hi there,
I’ve been trying for many days to integrate JasperReports into my Vaadin webapp, but without success, even if I followed similar forum threads answers. I always get a blank page without any report. I hope you can help me.
The code I used is this:
final Connection conn = helper.getJDBCConnectionPool().reserveConnection();
final HashMap map = new HashMap();
StreamResource.StreamSource source = new StreamResource.StreamSource() {
@Override
public InputStream getStream() {
byte[] b = null;
try {
InputStream rep = getClass().getClassLoader().getResourceAsStream("reports/myreport.jasper");
if (rep == null) {
getWindow().showNotification("No report!");
} else {
JasperReport report = (JasperReport) JRLoader.loadObject(rep);
b = JasperRunManager.runReportToPdf(report, null, conn);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return new ByteArrayInputStream(b);
}
};
StreamResource resource = new StreamResource(source, "report.pdf", getApplication());
resource.setMIMEType("application/pdf");
resource.setCacheTime(0);
getApplication().getMainWindow().open(resource, "_new");
conn.commit();
helper.getJDBCConnectionPool().releaseConnection(conn);
Another try I did is this:
JasperPrint print = JasperFillManager.fillReport(getClass().getClassLoader().getResourceAsStream("reports/myreport.jasper"), map, conn);
JRHtmlExporter exporter = new JRHtmlExporter();
final ByteArrayOutputStream output=new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,output);
exporter.exportReport();
output.flush();
StreamResource.StreamSource source = new StreamResource.StreamSource() {
public InputStream getStream() {
byte[] b = null;
b=output.toByteArray();
return new ByteArrayInputStream(b);
}
};
StreamResource resource = new StreamResource(source, "report.html", getApplication());
resource.setCacheTime(0);
getApplication().getMainWindow().open(resource, "_new");
conn.commit();
helper.getJDBCConnectionPool().releaseConnection(conn);