最近第一次使用jasper画报表,遇到中文无法显示的问题。通过在网上收集的材料,现总结一下几点:包括里边遇到的许多坑
1、首先在tibco jaspersoft开发时加入itex-asiain-5.2.0.jar;itext-asiancmaps-5.1.1.jar。导入lib库里;
2、在自己的jasper文件中的中文字符地方设置字符样式;
必须设置报表上各显示对象的相关属性,各属性设置说明如下:
Font name: 宋体 (中文字体)
PDF font name: STSong-Light
PDF Encoding: UniGB-UCS2-H(注意这个地方,如果使用UniGB-UCS2-V会显示字体竖着显示)
PDF Embeded: √
注意:此三项的设置必须确保iTextAsian.jar亚洲语言包已经包含在项目中,否则出现编译错误;
附上一段设置:
<staticText>
<reportElement x="80" y="0" width="370" height="40" uuid="e6f13996-a54b-49dd-aeb3-026650924bcb"/>
<textElement textAlignment="Center">
<font fontName="宋体" size="23" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
</textElement>
<text><![CDATA[出库单]]></text>
</staticText>
3、将itex-asiain-5.2.0.jar自己打包上传到自己的maven库里,打包方式参考:
注意:上传的是自己的私库,如果有账号密码记得修改自己本地的setting.xml,设置账号密码
<server>
<id>releases</id>
<username>userName</username>
<password>password</password>
</server>
<server>
<id>snapshots</id>
<username>userName</username>
<password>password</password>
</server>
4、自己pom文件引用的依赖
<!-- 生成pdf所依赖的包 -->
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.11</version>
</dependency>
<!-- 这个是我自己打包上传的依赖包-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextasian</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.lowagie/itextasian -->
<!-- https://mvnrepository.com/artifact/com.itextpdf/font-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.1.10</version>
<scope>test</scope>
</dependency>
5、发布到linux上,找不到字符集,参考解决方案
Font '宋体' is not available to the JVM. See the Javadoc for more details
1、把需要用到的字体(可以直接拷贝windows系统的C:\WINDOWS\Fonts 下的相关字体)拷贝当前项目的classpath下,一般为classes目录下
2、在classpath里添加 jasperreports.properties 属性文件
文件内容为:
net.sf.jasperreports.awt.ignore.missing.font=true
6、附上一段代码供参考
public void printApplyOrder(@RequestParam(name = "orderId",required = true) Long orderId,BladeUser user) {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
JasperPrint jasperPrint;
ResApplyDetailVO applyDetailVO = resOrderService.resApplyOrderDetail(orderId);
if (null == applyDetailVO) {
throw new ServiceException(resManageService.getExceptionMsg(InventoryConstant.ExceptionMsg.KEY_INVENTORY_ORDER));
}
try {
InputStream inputStream = this.getClass().getResourceAsStream("/jasper/ResApplyOrder.jrxml");
List<ResOrderDtlVO> resOrderDtlVOS = applyDetailVO.getResOrderDtlVOList();
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(resOrderDtlVOS);
Map<String,Object> params = new HashMap<>();
if (null != user) {
R<Tenant> tenantR = sysClient.getTenant(user.getTenantId());
if (null != tenantR&& ResultCodeConstant.ResponseCode.SUCCESS == tenantR.getCode() && null != tenantR.getData()){
params.put("companyName",tenantR.getData().getTenantName());
}
params.put("operaterName",user.getUserName());
}
params.put("orderId",applyDetailVO.getOrderId());
params.put("applyName",applyDetailVO.getApplyName());
params.put("operateTime", TimeUtil.getFormattedDate(TimeUtil.getSysDate(),TimeUtil.YYYY_MM_DD_HH_MM_SS));
params.put("departmentName", applyDetailVO.getApplyDepartment());
params.put("inventoryName",dictClient.getValue(InventoryConstant.StorageName.CODE,InventoryConstant.StorageName.LOCAL_STORAGE).getData());
params.put("totalNum",resOrderDtlVOS.size());
jasperPrint = JasperFillManager.fillReport(JasperCompileManager.compileReport(inputStream), params, dataSource);
response.setCharacterEncoding("UTF-8");
response.setDateHeader("Expires", 0); // 清除页面缓存
// response.setHeader("Content-Disposition",
// "attachment;" + "filename=" + new String((fileName + "." + suffix).getBytes(), "ISO-8859-1"));
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","inline;filename=storeOut.pdf");
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
pdfExporter.exportReport();
}catch (Exception e) {
log.error(e.getMessage(), e);
}
}
本文详细介绍了在使用JasperReports开发报表时遇到的中文显示问题及其解决方案,包括引入itext-asiain等依赖、设置字符样式及字体属性、打包上传至私库的过程,并提供代码示例。
2913

被折叠的 条评论
为什么被折叠?



