Nov*_*aax 5 java jpeg jasper-reports
我正在寻找一种JRGraphics2DExporter将报告导出为 JPG 的方法。
有没有可能做到这一点JRGraphics2DExporter?
您想使用JRGraphics2DExporter,但这也可以直接使用JasperPrintManager
考虑每页使用多个图像 1 的代码示例
//Get my print, by filling the report
JasperPrint jasperPrint = JasperFillManager.fillReport(report, map,datasource);
final String extension = "jpg";
final float zoom = 1f;
String fileName = "report";
//one image for every page in my report
int pages = jasperPrint.getPages().size();
for (int i = 0; i < pages; i++) {
try(OutputStream out = new FileOutputStream(fileName + "_p" + (i+1) + "." + extension)){
BufferedImage image = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint, i,zoom);
ImageIO.write(image, extension, out); //write image to file
} catch (IOException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
如果你喜欢一张图片包含所有页面,你应该isIgnorePagination="true"在 jasperReport 标签上设置
您可以指示导出器将报告转储到内存中的图像,然后将其保存到磁盘。
创建图像(设置正确的宽度、高度和格式):
BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
Run Code Online (Sandbox Code Playgroud)
创建导出器,配置它(也许应该设置一些其他参数)并导出报告:
JRGraphics2DExporter exporter = new JRGraphics2DExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, (Graphics2D)image.getGraphics());
exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, Float.valueOf(1));
exporter.exportReport();
Run Code Online (Sandbox Code Playgroud)
将图像转储到磁盘:
ImageIO.write(image, "PNG", new File("image.png"));
Run Code Online (Sandbox Code Playgroud)