Решение:
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);
}
});
Полный код:
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());
}
}