Search code examples
javahtmljspreportingjasper-reports

Export to single HTML with embedded images using Jasper Report


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.


Solution

  • A solution:

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