使用IText导出PDF意外发现其他外文都能导出成功,只有泰文会消失,查了一下没有能用的办法,官网也没有我这种情况,最后还是误打误撞试出来的。还是要下载泰文字体,网上很多,我是从这里下载的:https://fontmeme.com/ziti/thai-fonts/
导出pdf的方法,使用的是html当模版,重要的是getConverterProperties方法。
java
//生成PDF文件至文件服务器
private BaseFileInfoResp uploadPdfToFileServer(PoInfo poInfo) {
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
Map<String, Object> stringObjectMap = inParamChange(poInfo);
stringObjectMap.putAll(PmsPdfFieldEnum.getMap());
log.info("json: {}", JSONUtil.toJsonStr(stringObjectMap));
Configuration freemarkerCfg = new Configuration(Configuration.VERSION_2_3_28);
freemarkerCfg.setTemplateLoader(new ClassTemplateLoader(ExcelServiceImpl.class, "/template/"));
Template template = freemarkerCfg.getTemplate("pdf.html");
StringWriter out = new StringWriter();
template.process(stringObjectMap, out);
String html = out.toString();
PdfDocument pdf = new PdfDocument(new PdfWriter(output));
pdf.setDefaultPageSize(PageSize.A4);
Document document = HtmlConverter.convertToDocument(html, pdf, getConverterProperties());
document.getRenderer().close();
document.close();
MultipartFile multipartFile = MultipartFileUtils.buildFileClientFile(String.format("%s_%s.pdf", poInfo.getCode(), poInfo.getCurrentVersion()), output.toByteArray());
// 文件上传fastDFS再返回文件地址
return fileService.upload(multipartFile, userService.getCurrentUserDto().getUserAccount());
} catch (Exception e) {
log.info("生成pdf异常:", e);
throw new BizException(I18nStaticEnum.static_text_112381.code);
}
}
getConverterProperties()原代码:
java
/**
* 解决中文字体问题
* @return
* @throws Exception
*/
private ConverterProperties getConverterProperties() throws Exception {
ConverterProperties props = new ConverterProperties();
FontProvider fontProvider = new FontProvider();
PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
fontProvider.addFont(sysFont.getFontProgram(), "UniGB-UCS2-H");
fontProvider.addStandardPdfFonts();
fontProvider.addFont("template/font/simsun.ttc");
fontProvider.addFont("template/font/STHeitibd.ttf");
props.setFontProvider(fontProvider);
props.setCharset("utf-8");
return props;
}
getConverterProperties()修改后的代码:
java
/**
* 解决中文字体问题
* @return
* @throws Exception
*/
private ConverterProperties getConverterProperties() throws Exception {
ConverterProperties props = new ConverterProperties();
FontProvider fontProvider = new FontProvider();
// 一定要注释这里,就是UniGB-UCS2-H这个编码格式和泰语编码格式不兼容
// PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
// fontProvider.addFont(sysFont.getFontProgram(), "UniGB-UCS2-H");
fontProvider.addStandardPdfFonts();
fontProvider.addFont("template/font/simsun.ttc");
fontProvider.addFont("template/font/STHeitibd.ttf");
fontProvider.addFont("template/font/STSONG.TTF"); //华文宋体
fontProvider.addFont("template/font/NotoSansThai.ttf"); //泰语字体
props.setFontProvider(fontProvider);
props.setCharset("utf-8");
return props;
}
附上jar包版本:
xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--itext7 html转pdf用到的包-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<!--itext7 中文支持-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.1.12</version>
</dependency>