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了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章