我有这段代码只能根据给定条件一次只生成一份Jasper报告为PDF。但是,如果我想将两个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).
最佳答案
这是执行此操作的旧方法:
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_LIST
和JRPdfExporterParameter.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));