Jasper Report 可以导出为带有嵌入图像的单个 HTML 吗?
我将 jasper 报告输出为单个 Excel 文件、PDF、RTF。 但是可以多人播放 HTML 文件。 我管理的不是单个报告文件,而是 HTML 格式的许多文件和文件夹对我来说很麻烦。
最佳答案
解决方案:
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());
}
}
关于java - 使用 Jasper Report 导出为带有嵌入图像的单个 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6942373/