解決Linux下圖片水印亂碼以及生成PDF亂碼問題

1 問題定位

windows下面無問題,在linux下面出現問題,中文變成方框,經過排查發現linux下缺少字體,只需將widows字體上傳到linux服務器進行配置加載就好

2 解決方案

2.1 方案一

2.1.1 安裝字體庫

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n7" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost ~]# yum -y install fontconfig</pre>

2.1.2 新建目錄

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n9" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost ~]# /usr/share/fonts
[root@localhost fonts]# mkdir chinese
[root@localhost ~]# cd chinese</pre>

2.1.2 上傳Windows字體文件

將C:\Windows\Fonts下面的所有文件,上傳到服務器的/usr/share/fonts/chinese目錄下

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n13" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost chinese]# ll
total 185340
-rwxr-xr-x 1 root root 980756 Jan 20 16:10 arialbd.ttf
-rwxr-xr-x 1 root root 721144 Jan 20 16:10 arialbi.ttf
-rwxr-xr-x 1 root root 717428 Jan 20 16:10 ariali.ttf
-rwxr-xr-x 1 root root 1036584 Jan 20 16:10 arial.ttf
-rwxr-xr-x 1 root root 167592 Jan 20 16:10 ariblk.ttf
-rwxr-xr-x 1 root root 40340 Jan 20 16:10 Arvo-Regular.ttf
-rwxr-xr-x 1 root root 131660 Jan 20 16:10 AxureHandwriting-BoldItalic.otf
-rwxr-xr-x 1 root root 138388 Jan 20 16:10 AxureHandwriting-Bold.otf
-rwxr-xr-x 1 root root 68244 Jan 20 16:10 AxureHandwriting-Italic.otf
-rwxr-xr-x 1 root root 64288 Jan 20 16:10 AxureHandwriting.otf
-rwxr-xr-x 1 root root 322864 Jan 20 16:10 bahnschrift.ttf
-rwxr-xr-x 1 root root 229408 Jan 20 16:10 comicbd.ttf
...</pre>

2.1.3 安裝字體

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n15" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost chinese]# yum -y install ttmkfdir </pre>

2.1.4 刷新字體緩存

進行這個操作之后,就不用重啟機器

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n19" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost chinese]# fc-cache</pre>

2.1.5 查看安裝字體

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n21" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost ~]# fc-list
/usr/share/fonts/chinese/cour.ttf: Courier New:style=Standaard,Regular
/usr/share/fonts/chinese/consolai.ttf: Consolas:style=Italic
/usr/share/fonts/chinese/monbaiti.ttf: Mongolian Baiti:style=Regular
/usr/share/fonts/chinese/georgiai.ttf: Georgia:style=Italic,Cursiva
/usr/share/fonts/chinese/impact.ttf: Impact:style=Regular,Standard
/usr/share/fonts/chinese/PoiretOne-Regular.ttf: Poiret One:style=Regular
/usr/share/fonts/chinese/palabi.ttf: Palatino Linotype:style=Negreta cursiva,Bold Italic
/usr/share/fonts/chinese/ntailu.ttf: Microsoft New Tai Lue:style=Regular
/usr/share/fonts/chinese/himalaya.ttf: Microsoft Himalaya:style=Regular
/usr/share/fonts/chinese/Roboto-Regular.ttf: Roboto:style=Regular
/usr/share/fonts/chinese/LucidaBrightRegular.ttf: Lucida Bright:style=Regular
/usr/share/fonts/chinese/phagspa.ttf: Microsoft PhagsPa:style=Regular
/usr/share/fonts/chinese/trebucbd.ttf: Trebuchet MS:style=Bold,Negrita
/usr/share/fonts/chinese/gadugi.ttf: Gadugi:style=Regular
/usr/share/fonts/chinese/palai.ttf: Palatino Linotype:style=Cursiva,Italic
/usr/share/fonts/chinese/bahnschrift.ttf: Bahnschrift:style=Bold SemiCondensed
/usr/share/fonts/chinese/bahnschrift.ttf: Bahnschrift:style=Light
/usr/share/fonts/chinese/bahnschrift.ttf: Bahnschrift:style=SemiLight SemiCondensed
/usr/share/fonts/chinese/ntailub.ttf: Microsoft New Tai Lue:style=Bold
/usr/share/fonts/chinese/consola.ttf: Consolas:style=Regular
/usr/share/fonts/chinese/ariali.ttf: Arial:style=Πλ?για,Italic
/usr/share/fonts/chinese/bahnschrift.ttf: Bahnschrift:style=Bold Condensed
...</pre>

2.2 方案二

2.2.1 JDK目錄

JDK目錄 /opt/module/jdk1.8.0_144

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost jre]# echo $JAVA_HOME
/opt/module/
[root@localhost jre]# cd /opt/module/
[root@localhost module]# ll
total 12
drwxr-xr-x 7 root root 4096 Jul 22 2017 jdk1.8.0_144
[root@localhost module]#</pre>

2.2.2 創建目錄

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost jre]# cd /opt/module/jdk1.8.0_144/jre/lib/fonts
[root@localhost fonts]# mkdir fallback
[root@localhost fonts]# cd fallback</pre>

2.2.3 上傳Windows字體文件

將C:\Windows\Fonts下面的所有文件,上傳到服務器的/opt/module/jdk1.8.0_144/jre/lib/fonts/fallback目錄下

<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n33" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[root@localhost fallback]# ll
total 185340
-rwxr-xr-x 1 root root 980756 Jan 20 16:10 arialbd.ttf
-rwxr-xr-x 1 root root 721144 Jan 20 16:10 arialbi.ttf
-rwxr-xr-x 1 root root 717428 Jan 20 16:10 ariali.ttf
-rwxr-xr-x 1 root root 1036584 Jan 20 16:10 arial.ttf
-rwxr-xr-x 1 root root 167592 Jan 20 16:10 ariblk.ttf
-rwxr-xr-x 1 root root 40340 Jan 20 16:10 Arvo-Regular.ttf
-rwxr-xr-x 1 root root 131660 Jan 20 16:10 AxureHandwriting-BoldItalic.otf
-rwxr-xr-x 1 root root 138388 Jan 20 16:10 AxureHandwriting-Bold.otf
-rwxr-xr-x 1 root root 68244 Jan 20 16:10 AxureHandwriting-Italic.otf
-rwxr-xr-x 1 root root 64288 Jan 20 16:10 AxureHandwriting.otf
-rwxr-xr-x 1 root root 322864 Jan 20 16:10 bahnschrift.ttf
-rwxr-xr-x 1 root root 229408 Jan 20 16:10 comicbd.ttf</pre>

2.3 方案三

如果能確定項目中使用的字體對應的字體文件,只需上傳單個文件,比如宋體 對應的字體文件就是 simsun.ttc,這樣只需操作一個文件的上傳就可以完成問題解決

3 重啟服務

重啟應用服務,比如tomcat等

4 相關資源

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容