SpringBoot + JasperReport6.x 集成报表控件,导出PDF汉字无法显示,控制台提示如下错误信息:
2020-07-30 09:27:17.539 [http-nio-9092-exec-1] WARN net.sf.jasperreports.engine.query.JRJdbcQueryExecuter - The supplied java.sql.Connection object is null.
SpringBoot 集成JasperReport6.x 之相关依赖:
org.springframework.boot
spring-boot-starter-web
net.sf.jasperreports
jasperreports
6.10.0
com.lowagie
itext
2.1.7.js4
org.codehaus.groovy
groovy-all
org.mozilla
javascript
1.7.2
org.springframework
spring-context-support
org.projectlombok
lombok
指定JasperReport6.x 报表字体:
项目结构:
jasperreports_extension.properties文件内容如下:
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.lobstertwo=fonts/fonts.xml
fonts.xml配置内容如下:
fonts/STSONG.TTF
fonts/STSONG.TTF
fonts/STSONG.TTF
fonts/STSONG.TTF
Identity-H
true
STS, Arial, Helvetica, sans-serif
STS, Arial, Helvetica, sans-serif
针对控制台提示信息,需要修改的代码片段:
jasperreport6.x 输出pdf 文件核心代码:
@RequestMapping("/one")
public void getReportByParam(HttpServletResponse response) {
ServletOutputStream sosRef = null;
// bean 连接获取
try {
Map map = new HashMap();
map.put("quarter", "第一季度");
map.put("personUseCount", "111");
map.put("archCount", "99");
map.put("personCount", "74");
map.put("textNumbs", "59/38");
map.put("copyDrawingCount", "99");
// 获取文件流
ClassPathResource resource = new ClassPathResource("jasper" + File.separator + "Blank_A4_1.jasper");
InputStream jasperStream = resource.getInputStream();
sosRef = response.getOutputStream();
// 重点代码
JasperRunManager.runReportToPdfStream(jasperStream, sosRef, map, new JREmptyDataSource());
response.setContentType("application/pdf");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
sosRef.flush();
sosRef.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在标记为重点代码片段,我之前写了一种错误写法导致,控制台输出相关错误信息:
WARN net.sf.jasperreports.engine.query.JRJdbcQueryExecuter - The supplied java.sql.Connection object is null.
错误代码:
JasperRunManager.runReportToPdfStream(jasperStream, sosRef, map);
正确代码:
JasperRunManager.runReportToPdfStream(jasperStream, sosRef, map, new JREmptyDataSource());
报表显示效果: