JasperReport6.x +Spring Boot 导出PDF汉字不显示,提示:net.sf.jasperreports.engine.query.JRJdbcQueryExecuter

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());

报表显示效果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值