JRGraphics2DExporter - 如何将报告导出为JPG?

时间:2017-01-25 13:52:40

标签: java jasper-reports jpeg

我正在使用JRGraphics2DExporter搜索方法,将报告导出为JPG。

使用JRGraphics2DExporter是否有任何可能性?

2 个答案:

答案 0 :(得分:1)

您可以指示导出器将报告转储到内存中的映像,然后将其保存到磁盘。

创建图像(设置适当的宽度,高度和格式):

BufferedImage image = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);

创建导出器,对其进行配置(可能应设置其他一些参数)并导出报告:

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();

将图像转储到磁盘:

ImageIO.write(image, "PNG", new File("image.png"));

答案 1 :(得分:0)

您想使用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();
    }
}

如果您喜欢包含所有页面的1张图片,则应在jasperReport标记上设置isIgnorePagination="true"