我正在使用 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();
答案 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();