gpt4 book ai didi

java - 将 JasperReport 导出为 PDF 输出流?

转载 作者:IT老高 更新时间:2023-10-28 21:10:13 25 4
gpt4 key购买 nike

我正在编写一个非常简单的示例项目来熟悉 Jasper Reports。我想将我配置的报告导出为 PDF OutputStream,但没有工厂方法:

InputStream template = JasperReportsApplication.class
.getResourceAsStream("/sampleReport.xml");
JasperReport report = JasperCompileManager.compileReport(template);
JasperFillManager.fillReport(report, new HashMap<String, String>());
// nope, just chuck testa.
//JasperExportManager.exportReportToPdfStream(report, new FileOutputStream(new File("/tmp/out.pdf")));

如何在 OutputStream 中获取 PDF?

最佳答案

好的,这就是它的工作原理; JasperFillManager 实际上返回一个 JasperPrint 对象,所以:

// get the JRXML template as a stream
InputStream template = JasperReportsApplication.class
.getResourceAsStream("/sampleReport.xml");
// compile the report from the stream
JasperReport report = JasperCompileManager.compileReport(template);
// fill out the report into a print object, ready for export.
JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, String>());
// export it!
File pdf = File.createTempFile("output.", ".pdf");
JasperExportManager.exportReportToPdfStream(print, new FileOutputStream(pdf));

享受。

关于java - 将 JasperReport 导出为 PDF 输出流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com