FacesContext facesContext = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
File reportFile = new File(servletContext.getRealPath("/reports/produtos.jasper"));
InputStream reporte = null;
ServletOutputStream ouputStream = null;
try {
reporte = new FileInputStream(reportFile);
HashMap param = new HashMap();
//o metodo createReportDataSource esta retornando uma lista de objetos a serem exibidos
JasperPrint jp = JasperFillManager.fillReport(reporte, param, this.createReportDataSource());
byte[] bytes = JasperExportManager.exportReportToPdf(jp);
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=produtos.pdf");
ouputStream = response.getOutputStream();
JRPdfExporter exporterPdf = new JRPdfExporter();
exporterPdf.setParameter(JRPdfExporterParameter.JASPER_PRINT ,jp);
exporterPdf.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, ouputStream);
exporterPdf.exportReport();
facesContext.responseComplete();
ouputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ouputStream != null) ouputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
O grande problema eh que quando executo no IE nada eh exibido no browser, mas quando uso o FireFox
a tela exibe uma pagina em branco com dados estranhos.
