将 JasperPrint 直接流式传输为 PDF

Stream JasperPrint directly to PDF

是否可以将结果直接流式传输到 pdf 文件而不是先创建 JasperPrint 对象?我的 JasperPrint 对象可以变得非常大并且 java.lang.OutOfMemoryError 发生...

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, reportparameter.getAll(), dataSource);
// dead end
OutputStream output = new FileOutputStream(new File("C:/Users/bla/Desktop/test/JasperReport.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, output);

好吧,我找到了一个解决方案...首先将 JasperPrint 对象写入磁盘,然后您可以流式传输内容并通过流创建 PDF。 不再有内存问题,但您需要先将结果保存在磁盘上。

    OutputStream printOut = new FileOutputStream(new File("C:/Users/bla/Desktop/test/jasperprint.jasperprint"));
    JasperFillManager.fillReportToStream(jasperReport, printOut, reportparameter.getAll(), dataSource);
    printOut.close();

    OutputStream output = new FileOutputStream(new File("C:/Users/bla/test/JasperReport.pdf"));
    JasperExportManager.exportReportToPdfStream(new FileInputStream(new File("C:/Users/bla/Desktop/test/jasperprint.jasperprint")), output);
    output.close();