Jasper Reports:如何将两个报告流合并为一个PDF输出?

时间:2015-10-28 14:13:24

标签: java pdf jasper-reports inputstream

我有这个代码,它只根据给定的条件一次生成一个Jasper报告。但是,如果我想将两个Jaspers合并为一个PDF文件呢? (即PDF输出应为LoadTagR2R.jasper + LoadTagR2RLandscape.jasper - 这将是一个包含多个页面的PDF输出流)

        if (fullHalfTag.equalsIgnoreCase("Y")){ //Read FULL_HALF_TAG value (if FULL_HALF_TAG == 'Y')
            logger.debug("Printing FULL tag...");
            reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/LoadTagR2RLandscape.jasper");

        } else {
            logger.debug("Printing HALF tag...");
            reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/LoadTagR2R.jasper");

        }

        //Report parameters
        Map parameters = new HashMap();
        parameters.put("PLT_NO", pltNo);
        parameters.put("TICKET_NO", ticketNo);

          try {

             Connection conn = ConnectionUtils.getConnection();

             JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, parameters, conn);
             response.setContentType("application/pdf");

             servletOutputStream.flush();
             servletOutputStream.close();

我尝试合并两个输入流,如下所示,但它不起作用。想法?

        new java.io.SequenceInputStream(s1, s2).

1 个答案:

答案 0 :(得分:4)

这是执行此操作的旧方法:

JasperPrint jasperPrint1 = JasperFillManager.fillReport(reportStream1,  parameters, conn);
JasperPrint jasperPrint2 = JasperFillManager.fillReport(reportStream2,  parameters, conn);

List<JasperPrint> list = new ArrayList<JasperPrint>();
list.add(jasperPrint1);
list.add(jasperPrint2);

JRPdfExporter exporter = new JRPdfExporter();

exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, list);
exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM,servletOutputStream); 
exporter.exportReport();

编辑:

JasperReports 6.1.1 API JRPdfExporterParameter.JASPER_PRINT_LISTJRPdfExporterParameter.OUTPUT_STREAM 已弃用添加新方法。

替换:

exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT_LIST, list);
exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM,servletOutputStream);

exporter.setExporterInput(SimpleExporterInput.getInstance(list));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(servletOutputStream));