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

发布时间 2023-12-17 14:56:30作者: 清清飞扬

From: https://www.cnblogs.com/mabiao008/p/17339307.html

本地没出现这个问题,到了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);
}
}
复制代码

 

因服务在k8s集群中,不知道会飘到哪个机器中,所以把集群都安装下字体

scp -r /usr/share/fonts/win @172.31.160.55:/usr/share/fonts

 

如果遇到:fc-cache -bash: fc-cache: command not found

先安装必要程序

# 使mkfontscale和mkfontdir命令正常运行
yum -y install mkfontscale
# 使fc-cache命令正常运行。如果提示 fc-cache: command not found
yum -y install fontconfig
把文件放进/usr/share/fonts就可以了

然后在fonts路径运行

mkfontscale
mkfontdir
fc-cache -fv

 

最后,重启服务。经过测试ok了