环境:springboot2.0 Jaspereport 6.8.0 ,
在之前的增加二维码显示的依赖包
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.1</version>
</dependency>
<!--addnew -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-bridge</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>cn.lesper</groupId>
<artifactId>iTextAsian</artifactId>
<version>3.0</version>
</dependency>
在使用HtmlExporter生成在线预览的时候不显示二维码 不显示的原因可能
返回的img的src的找不到,应用在启动的时候没用注册
net.sf.jasperreports.j2ee.servlets.ImageServlet
监听,在没用注册此监听的情况下,注册监听的方法待验证,
使用img src 流的方式在页面上显示QRCODE解决在页面不显示二维码的问题
关键代码片段:
预览:
public void previewReport(String modelId, HttpServletResponse response) throws Exception {
/* //exportByJson(response,modelId,ReportUtil.PDF_TYPE,"",null);
testjDBC(modelId,response);*/
//jxrmxl的ReportModel
//生成HTML 预览用这种模式
ReportModel reportModel = reportModelService.findById(modelId).get();
//添加导出记录
//创建 JasperReport 对象
JasperReport jasperReport = createJasperReport(reportModel);
JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), jrEmptyDataSource);
ReportUtil.exportHtml(response, jasperPrint);
//ReportUtil.exportPdf(response,"test1",jasperPrint);
}
/**
* 创建 JasperReport 对象
*
* @param reportModel
* @return
* @throws Exception
*/
@Override
public JasperReport createJasperReport(ReportModel reportModel) throws Exception {
JasperReport jasperReport;
if (null == reportModel.getFileByte()) {
throw new Exception("文件不存在!");
}
/*File reportFile = new File(reportModel.getFilePath());
if (!reportFile.exists())
throw new Exception("模板文件不存在");*/
if (reportModel.getType() == 1) {
//编译,并得到jasperReport对象,相当于.jasper文件
jasperReport = JasperCompileManager.compileReport(new ByteArrayInputStream(reportModel.getFileByte()));
} else {
//直接加载编译后的模板文件
jasperReport = (JasperReport) JRLoader.loadObject(new ByteArrayInputStream(reportModel.getFileByte()));
}
return jasperReport;
}
/**
* 以html的形式打印报表
*
* @param response
* @param jasperPrint 打印对象
* @throws Exception
*/
public static void exportHtml(HttpServletResponse response, JasperPrint jasperPrint) throws Exception {
response.setContentType("text/html");//以页面的形式打开
/*JRHtmlExporter jrHtmlExporter = new JRHtmlExporter();
jrHtmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);//解决错乱显示img标签问题
jrHtmlExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
jrHtmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
jrHtmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jrHtmlExporter.exportReport();*/
Map<String, String> images = new HashMap<>();
HtmlExporter exporter=new HtmlExporter();
String[] propertyNames = jasperPrint.getPropertyNames();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
// exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
SimpleHtmlReportConfiguration simpleHtmlReportConfiguration = new SimpleHtmlReportConfiguration();
//将svg转换成img(png)格式
simpleHtmlReportConfiguration.setConvertSvgToImage(true);
SimpleHtmlExporterConfiguration reportExportConfiguration =new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(reportExportConfiguration );
exporter.setConfiguration(simpleHtmlReportConfiguration);
//ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ServletOutputStream outputStream = response.getOutputStream();
SimpleHtmlExporterOutput simpleHtmlExporterOutput = new SimpleHtmlExporterOutput(response.getOutputStream());
//生成imgsrc为base64格式的资源
simpleHtmlExporterOutput.setImageHandler(new HtmlResourceHandler() {
@Override
public void handleResource(String id, byte[] data) {
System.err.println("id" + id);
images.put(id, "data:image/png;base64," + Base64.encodeBytes(data));
}
@Override
public String getResourcePath(String id) {
return images.get(id);
}
});
exporter.setExporterOutput(simpleHtmlExporterOutput);
exporter.exportReport();
}