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 for me to manage not single report file, but many files and folders in HTML case.
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 for me to manage not single report file, but many files and folders in HTML case.
I don't think that jasper reports has built in support for this, so you'd have to roll out your own implementation. You can use this technique to embed them images.
So first you'd use java's xml parser to find all the image tags in the html http://www.mkyong.com/tutorials/java-xml-tutorials/. Then you'd read all the files, convert them to base64 string http://www.xinotes.org/notes/note/736/ and replace the
img
's src with the above format.A minor improvement to Dave Jarvis solution.
Instead of hard-coding the mime type in
You can try to discover the mime type like this:
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.To check, if you now generate an HTML report and inspect the source, you should see something like
<img href="http://www.mywebsite.com/report/image?image=img_0_0_2.png" />
.Now you need to activate the
ImageServlet
, so it can intercept and fulfill the image requests. Add the following block to yourweb.xml
file:(Note that the
/report/image
path matches the URL argument we passed to theWebHtmlResourceHandler
.)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
thatThe last thing to do is to add the
JasperPrint
object to the session, so theImageServlet
knows which image to serve. In JSP that can be done like this:Now it should work.
A solution:
Full code:
As noted by ilia, until recently data uri was not cross compatible, so you HAD to save multiple files. Might want to file an enhancement request with Jasper to request an option to save html as one file with embedded data uris