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 之相关依赖:
<dependencies>
<!--springboot web 基础模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot 集成jasperreport -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.10.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7.js4</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>javascript</artifactId>
<version>1.7.2</version>
</dependency>
<!-- spring 框架依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- lombok 框架依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
指定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配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<fontFamily name="STS">
<normal>fonts/STSONG.TTF</normal>
<bold>fonts/STSONG.TTF</bold>
<italic>fonts/STSONG.TTF</italic>
<boldItalic>fonts/STSONG.TTF</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">STS, Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">STS, Arial, Helvetica, sans-serif</export>
</exportFonts>
</fontFamily>
</fontFamilies>
针对控制台提示信息,需要修改的代码片段:
jasperreport6.x 输出pdf 文件核心代码:
@RequestMapping("/one")
public void getReportByParam(HttpServletResponse response) {
ServletOutputStream sosRef = null;
// bean 连接获取
try {
Map<String, Object> map = new HashMap<String, Object>();
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());
报表显示效果: