JasperReport6.x +Spring Boot 导出PDF汉字不显示,提示:Unpatched iText found, cannot use glyph rendering

虾米哥 阅读:1747 2021-03-31 13:46:34 评论:0

SpringBoot + JasperReport6.x 集成报表控件,导出PDF汉字无法显示,控制台提示如下错误信息:

 WARN  net.sf.jasperreports.engine.export.PdfGlyphRenderer - Unpatched iText found, cannot use glyph rendering

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>

Jaspersoft@Studio 设置相关报表字体:

造成控制台输出相关提示信息:

WARN  net.sf.jasperreports.engine.export.PdfGlyphRenderer - Unpatched iText found, cannot use glyph rendering

原因如下:

https://mvnrepository.com/ 中没有找到itext2.1.17.js7 版本下载,使用https://search.maven.org/ 检索到itext2.1.17 版本作为依赖下载本地,执行代码时出现上述错误信息。最后在jasperreport 开源的maven 仓库找到itext2.1.17.js4: http://jasperreports.sourceforge.net/maven2/com/lowagie/itext/2.1.7.js4/

核心功能代码:

@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()); 
			JasperRunManager.runReportToPdfStream(jasperStream, sosRef, map); 
			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(); 
			} 
		} 
	}

依赖itext2.1.17.js 代码下载:

报表下载文件下载地质:

标签:Spring
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号