如何查看从 JapserReport 生成的 PDF 到 Primefaces 媒体组件?

How to view generated PDF from JapserReport to Primefaces Media Component?

我有以下方法从 Jasper 生成我的 PDF 作为 byte[]。

    @Named
@ViewScoped
public class ReportingBean extends BasicBean {
...
    public void generateUebersichtMaterial() throws FileNotFoundException {
        InputStream pathMaster_jrxml;
        InputStream pathSubReport_jrxml;
        pathMaster_jrxml = getClass().getResourceAsStream("/reports/mainreport.jrxml");
        pathSubReport_jrxml = getClass().getResourceAsStream("/reports/subreport.jrxml");
        try {
            getConnection();
            HashMap<String, Object> params = new HashMap<>();
            List<ReportMaterialUebersicht> list = new ArrayList<>();
            list = reportFacade.getReportMaterialUebersicht();
            JasperReport subreport = JasperCompileManager.compileReport(pathSubReport_jrxml);
            JasperReport report = JasperCompileManager.compileReport(pathMaster_jrxml);
            params.put(JRParameter.REPORT_LOCALE, Locale.FRENCH);
            params.put("subReport", subreport);
            JasperPrint jasperPrint = JasperFillManager.fillReport(report, params,
                    new JRBeanArrayDataSource(list.toArray()));
            connection.close();
            content = JasperExportManager.exportReportToPdf(jasperPrint);
            Faces.sendFile(content, "Materialuebersicht.pdf", false);
        } catch (Exception e) {
            Logger.getLogger(ReportingBean.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    ...
}

我想使用 primefaces 媒体组件,而不是在新的 window 中打开 PDF:

<p:media value="#{reportingBean.myPDF}" width="100%" height="100%" player="pdf" />

如何做到这一点?

我试过了,我通过使用 Primefaces 的 StreamedContent 自己找到了解决方案。

将这两行添加到 ReportBean:

InputStream in = new ByteArrayInputStream(content);
myDoc = new DefaultStreamedContent(in, "application/pdf", "test.pdf");

在 Bean 的 PostConstruct 中,我初始化变量 myPDF 并从媒体组件调用它。

<p:media value="#{reportingBean.myPDF}" width="100%" height="100%" player="pdf" />

这是完整的 Bean 示例:

ReportingBean:

@Named
@ViewScoped
public class ReportingBean extends BasicBean {
...
private DefaultStreamedContent myPDF;
    public void generateUebersichtMaterial() throws FileNotFoundException {
        InputStream pathMaster_jrxml;
        InputStream pathSubReport_jrxml;
        pathMaster_jrxml = getClass().getResourceAsStream("/reports/mainreport.jrxml");
        pathSubReport_jrxml = getClass().getResourceAsStream("/reports/subreport.jrxml");
        try {
            getConnection();
            HashMap<String, Object> params = new HashMap<>();
            List<ReportMaterialUebersicht> list = new ArrayList<>();
            list = reportFacade.getReportMaterialUebersicht();
            JasperReport subreport = JasperCompileManager.compileReport(pathSubReport_jrxml);
            JasperReport report = JasperCompileManager.compileReport(pathMaster_jrxml);
            params.put(JRParameter.REPORT_LOCALE, Locale.FRENCH);
            params.put("subReport", subreport);
            JasperPrint jasperPrint = JasperFillManager.fillReport(report, params,
                    new JRBeanArrayDataSource(list.toArray()));
            connection.close();
            content = JasperExportManager.exportReportToPdf(jasperPrint);
            Faces.sendFile(content, "Materialuebersicht.pdf", false);
        } catch (Exception e) {
            Logger.getLogger(ReportingBean.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    ...
    @PostConstruct
    private void init() {
        try {
            materialUebersichtPDF = generateUebersichtMaterial();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}