现在不推荐使用JRHtmlExporter.如何定义保存图像的路径? [英] JRHtmlExporter is deprecated now. How to define the path for saving images?
问题描述
现在不推荐使用 JRHtmlExporter 类( JasperReports 6.x ).
The JRHtmlExporter class is deprecated now (JasperReports 6.x).
我用 HtmlExporter 代替了此类的用法.但是我找不到替换exporter.setParameter (JRHtmlExporterParameter.IMAGES_URI, imageURI);
的等效函数.我需要设置存储用于生成报告( html 文件)的图像的路径.
I replaced the usage of this class with HtmlExporter. But I can not find equivalent function to replace exporter.setParameter (JRHtmlExporterParameter.IMAGES_URI, imageURI);
. I need to set path for storing images for generated report (html file).
我的旧代码:
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, filedReport);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
String imageURI = "q?srvAction=ReportImage&img="+returnFileName.substring(3).replace("/", "%2F")+"_files"+"%2F";
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,imageURI);
用于定义图像路径的 JasperReports 6.x 的实际代码是什么?
What will be the actual code for JasperReports 6.x for defining path to images?
推荐答案
我们从javadoc中可以看到 应使用HtmlExporterOutput.getImageHandler() 方法代替.
As we can see from the javadoc the JRHtmlExporterParameter.IMAGES_URI parameter is really deprecated and the HtmlExporterOutput.getImageHandler() method should be used instead of it.
我们可以使用 HtmlResourceHandler 接口,例如 WebHtmlResourceHandler .
We can use the implementation of HtmlResourceHandler interface, for example WebHtmlResourceHandler.
使用示例:
JRExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out);
output.setImageHandler(new WebHtmlResourceHandler("/reports/image?image={0}"));
exporter.setExporterOutput(output);
exporter.exportReport();
定义导出期间保存图像的位置
借助于 FileHtmlResourceHandler 处理程序,我们可以为生成的 html
使用示例:
JRExporter exporter = new HtmlExporter();
// output file for generated html report
File file = new File(String.format("./out/%1$s_%2$s.html", report.getTemplateName(), new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())));
ExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(file);
// the folder for storing images. It will be subfolder with name starting like generated html and ended with postfix "_images"
File resourcesDir = new File(file.getParent(), file.getName() + "_images");
// argument ({0}) will be replaced with the real image name
String pathPattern = resourcesDir.getName() + "/{0}";
exporterOutput.setImageHandler(new FileHtmlResourceHandler(resourcesDir, pathPattern));
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
生成的文件和文件夹将如下所示:
The generated files and folders will be like this:
.. [Folder]
image-test_20170504232649.html [File]
image-test_20170504232649.html_images [Folder]
img_0_0_0.png [File]
注释 :
Notes:
使用 HtmlResourceHandler 的示例可以找到 查看全文