Linux环境aspose插件word转pdf中文乱码解决方案

发布时间 2023-04-21 10:06:14作者: mabiao008

本地没出现这个问题,到了linux环境出现了这个问题。第一想到的是字体。

 

解决方案1:环境解决
安装字库,将win机器的c:\windows\fonts目录下的全部文件拷贝到生产服务器字体安装目录下,然后执行以下命令更新字体缓存。

查看linux目前的所有字体

fc-list
查看Linux目前的所有中文字体

fc-list :lang=zh

拷贝到linux下的字体目录
mkdir /usr/share/fonts/win
cd /usr/share/fonts 
sudo fc-cache -fv
执行命令让字体生效

source /etc/profile


解决方案2:代码解决
1.将window中字体放到linux中,上传至/usr/shared/fonts/chinese目录下,接下里用
2.在aspose代码中添加

@SneakyThrows
public static void wordToPdf(String wordPath, String pdfPath) {
getLicense();
File file = new File(pdfPath);
try (FileOutputStream os = new FileOutputStream(file)) {
OsInfo osInfo = SystemUtil.getOsInfo();
if(osInfo.isLinux()){
FontSettings.setFontsFolder("/usr/share/fonts/chinese", true);
}
Document doc = new Document(wordPath);
doc.save(os, SaveFormat.PDF);
}
}