Export to single HTML with embedded images using a Jasper report

Can I export a Jasper report into single HTML with inline images?

I have jasper report output as a single Excel, PDF, RTF file. But multi-page HTML files. It’s hard for me to manage not just one report file, but many files and folders in the case of HTML.

+7
source share
4 answers

I don't think reports from jasper are built in to support this, so you have to deploy your own implementation. You can use this technique to embed their images.

<img src="data:image/png;base64,iVBORw0K... " /> 

So, first you have to use the java xml parser to find all image tags in html http://www.mkyong.com/tutorials/java-xml-tutorials/ . Then you should read all the files, convert them to base64 string http://www.xinotes.org/notes/note/736/ and replace img src with the above format.

+4
source

Decision:

 Map<String, String> images = new HashMap<>(); SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(outputStream); simpleHtmlExporterOutput.setImageHandler(new HtmlResourceHandler() { @Override public void handleResource(String id, byte[] data) { System.err.println("id" + id); images.put(id, "data:image/jpg;base64," + Base64.encodeBytes(data)); } @Override public String getResourcePath(String id) { return images.get(id); } }); 

Full code:

 package com.test.report; import java.io.ByteArrayOutputStream; import java.io.File; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRXmlDataSource; import net.sf.jasperreports.engine.export.HtmlExporter; import net.sf.jasperreports.engine.export.HtmlResourceHandler; import net.sf.jasperreports.export.SimpleExporterInput; import net.sf.jasperreports.export.SimpleHtmlExporterOutput; import net.sf.jasperreports.export.SimpleHtmlReportConfiguration; import org.apache.commons.io.FileUtils; import org.junit.Test; import org.olap4j.impl.Base64; import com.artech.reportservice.reports.ReportType; public class ReportTest { Map<String, String> images = new HashMap<>(); @Test public void test() throws Exception { // String outFileName = "test.html"; String xmlFileLocation = "/Users/skozlic/dev/VacationToolProject/wokspace/ReportService/src/test/resources/machineReportTestFile.xml"; JasperReport reportTemplate = ReportType.MPM.getReportTemplate(); JRXmlDataSource jrxmlds = ReportType.MPM.getReportDateSource(xmlFileLocation); JasperPrint jasperPrint = JasperFillManager.fillReport(reportTemplate, null, jrxmlds); HtmlExporter exporterHTML = new HtmlExporter(); SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint); exporterHTML.setExporterInput(exporterInput); SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration(); exporterHTML.setConfiguration(reportExportConfiguration); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(outputStream); simpleHtmlExporterOutput.setImageHandler(new HtmlResourceHandler() { @Override public void handleResource(String id, byte[] data) { System.err.println("id" + id); images.put(id, "data:image/jpg;base64," + Base64.encodeBytes(data)); } @Override public String getResourcePath(String id) { return images.get(id); } }); exporterHTML.setExporterOutput(simpleHtmlExporterOutput); exporterHTML.exportReport(); FileUtils.writeByteArrayToFile(new File("test.html"), outputStream.toByteArray()); } } 
+13
source

As noted in ilia, until recently, uri data was not cross-compatible, so you had to save several files. You might want to submit a promotion request using Jasper to request the ability to save html as a single file with embedded uris data

0
source

A slight improvement for Dave Jarvis.

Instead of hard coding, the mime type in

 images.put(id, "data:image/jpg;base64," + Base64.encodeBytes(data)); 

You can try to open the mime type as follows:

 // Find out the mime type final ByteArrayInputStream bis = new ByteArrayInputStream( data ); final String mimeType = URLConnection.guessContentTypeFromStream( bis ); // Convert to an embedded "data" url. final String base64Data = "data:"+mimeType+";base64,"+Base64.encodeBytes( data ); imagesMap.put( id, base64Data ); 
0
source

All Articles