Can Jasper Report export to single HTML with embedded images?
I have output of jasper reports as single Excel file, PDF, RTF. But multiplay HTML files. It trouble fo
I was grappling with this for the last few days, and finally solved it. My reports run in a web environment, so I was able to use the net.sf.jasperreports.j2ee.servlets.ImageServlet to serve the images. This requires a bit of setting up though.
Use the JRImageRenderer to render the images in the report itself:
...
where $F{image_data} is the binary image data.
When exporting the report, nominate WebResourceHandler as the HTML resource handler.
SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(byteArrayOutputStream);
HtmlResourceHandler imageHandler = new WebHtmlResourceHandler("https://www.mywebsite.com/report/image?image={0}");
exporterOutput.setImageHandler(imageHandler);
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
To check, if you now generate an HTML report and inspect the source, you should see something like .
Now you need to activate the ImageServlet, so it can
intercept and fulfill the image requests. Add the following block to
your web.xml file:
JasperImageServlet
net.sf.jasperreports.j2ee.servlets.ImageServlet
JasperImageServlet
/report/image
(Note that the /report/image path matches the URL argument we passed to the WebHtmlResourceHandler.)
Start the webserver and try to generate an HTML report once more. It still won't work of course, but copy the image's URL and paste it into your browser. You should get an error message from the ImageServlet that
No JasperPrint documents found on the HTTP session.
The last thing to do is to add the JasperPrint object to the session, so the ImageServlet knows which image to serve. In JSP that can be done like this:
JasperPrint jasperPrint = ...
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
Now it should work.