1 问题描述

在centos服务器使用aspose.word转换word文件为pdf的时候显示中文乱码,但是在win服务器上使用可以正常转换。

2 问题原因

通过查资料分析后确认是由于linux服务器缺少对应的字库导致文件转换出现乱码的。

3 解决方案1:环境解决

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

查看linux目前的所有字体

fc-list

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

fc-list :lang=zh

 拷贝到linux下的字体目录

mkdir /usr/share/fonts/win
cp /local/src/fonts/* /usr/share/fonts/win

执行安装字体命令

cd /usr/share/fonts
sudo mkfontscale
sudo mkfontdir 
sudo fc-cache -fv

执行命令让字体生效

source /etc/profile

如果安装失败,可以考虑修改字体权限

chmod 755 *.ttf

4 解决方案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);
    }
}

3.重启服务

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐