随笔 - 493  文章 - 0  评论 - 97  阅读 - 239万

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

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了

posted on   清清飞扬  阅读(2180)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
历史上的今天:
2015-12-17 PHP usort 使用用户自定义的比较函数对数组中的值进行排序
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示