JSF 2.0打开PDF报告(不保存)

时间:2012-08-23 07:07:03

标签: jsf primefaces jasper-reports

我正在使用 PrimeFaces

我有一个数据表包含命令链接pdf generate

当我点击此链接时,我想在新浏览器中打开 pdf

我的生成代码报告为 PDF

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/report/personReport.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(reportPath);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

ArrayList<PersonReportBean> arrayList = new ArrayList<PersonReportBean>();
arrayList.add(bean); // here bean populate with all data
JRBeanCollectionDataSource collectionDataSource = new JRBeanCollectionDataSource(arrayList);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<String, Object>(), collectionDataSource);
response.setContentType("application/pdf");
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport();
FacesContext.getCurrentInstance().responseComplete();

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

JasperExportManager.exportReportToPdfStream(jasperPrint,response.getOutputStream());

而不是:

JRPdfExporter exporter = new JRPdfExporter();   
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());   
exporter.exportReport();