Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JRHtmlExporter is deprecated now. How to define the path for saving images?

The JRHtmlExporter class is deprecated now (JasperReports 6.x).

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).

My old code:

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

What will be the actual code for JasperReports 6.x for defining path to images?

like image 967
Thuong Tran Avatar asked Dec 01 '25 14:12

Thuong Tran


1 Answers

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.

Define the path where images are storing

We can use the implementation of HtmlResourceHandler interface, for example WebHtmlResourceHandler.

The example of using:

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

Define where to save images during export

With help of FileHtmlResourceHandler handler we can set the path for images for generated html

The example of using:

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:

The sample of using HtmlResourceHandler can be found here

like image 72
Alex K Avatar answered Dec 04 '25 04:12

Alex K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!