0%

解决Linux下生成PDF中文乱码的问题

在linux下跑的程序生成pdf时候出现方格、乱码,初步判断原因应该是linux系统下缺少中文字体,我们需要从windows下将中文字体上传到linux下, 进行安装。

01

安装软件

1
yum -y install fontconfig xorg-x11-font-utils

查看系统里的目前已有字体

1
2
3
4
5
6
7
8
9
10
11
[root@iZ2ze49kwoa6pxxyyxdoa7Z ~]# fc-list

/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
/usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique
/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique

目测应该是没有中文字体相关的字体,我们将windows下的字体(C:\Windows\Fonts)打包上传到linux上(/usr/share/fonts)。我这里偷懒了,如果你知道自己用的那个字体,单独上传就行

安装字体

1
2
3
4
5
6
7
8
9
[root@iZ2ze49kwoa6pxxyyxdoa7Z fonts]# pwd
/usr/share/fonts
[root@iZ2ze49kwoa6pxxyyxdoa7Z fonts]# mkdir win
[root@iZ2ze49kwoa6pxxyyxdoa7Z fonts]# unzip Fonts.zip -d ./win
[root@iZ2ze49kwoa6pxxyyxdoa7Z fonts]# cd win/
#生成字体索引
[root@iZ2ze49kwoa6pxxyyxdoa7Z win]# mkfontscale
[root@iZ2ze49kwoa6pxxyyxdoa7Z win]# mkfontdir
[root@iZ2ze49kwoa6pxxyyxdoa7Z win]# fc-cache -fv

验证

再次查看系统里的字体,发现已经有很多新添加的了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@iZ2ze49kwoa6pxxyyxdoa7Z win]# fc-list
/usr/share/fonts/win/SitkaVF-Italic.ttf: Sitka,Sitka Text:style=Text Italic,Italic
/usr/share/fonts/win/DejaVuSansMono-BoldOblique_0.ttf: DejaVu Sans Mono:style=Bold Oblique
/usr/share/fonts/win/SitkaVF.ttf: Sitka,Sitka Text:style=Heading Bold
/usr/share/fonts/win/seguibli.ttf: Segoe UI,Segoe UI Black:style=Black Italic,Italic
/usr/share/fonts/win/COLONNA.TTF: Colonna MT:style=Regular,obyčejné
/usr/share/fonts/win/LTYPE.TTF: Lucida Sans Typewriter:style=Regular,normal
/usr/share/fonts/win/ebrima.ttf: Ebrima:style=Regular
/usr/share/fonts/win/GARA.TTF: Garamond:style=Regular,Standard
/usr/share/fonts/win/REFSAN.TTF: MS Reference Sans Serif:style=Regular,normal
/usr/share/fonts/win/TCM_____.TTF: Tw Cen MT:style=Regular,Κανονικά
/usr/share/fonts/win/Candarai.ttf: Candara:style=Italic
/usr/share/fonts/win/ROCC____.TTF: Rockwell Condensed:style=Regular,normal
/usr/share/fonts/win/msjh.ttc: Microsoft JhengHei UI:style=Regular
/usr/share/fonts/win/SitkaVF-Italic.ttf: Sitka,Sitka Text:style=Heading Bold Italic
/usr/share/fonts/win/STXIHEI.TTF: STXihei:style=Regular
/usr/share/fonts/win/FRAHVIT.TTF: Franklin Gothic Heavy:style=Italic,Kursivoitu
/usr/share/fonts/win/STZHONGS.TTF: STZhongsong:style=Regular
/usr/share/fonts/win/ROCKI.TTF: Rockwell:style=Italic,Kursivoitu
/usr/share/fonts/win/HPSimplified.ttf: HP Simplified:style=Bold
……

过滤一下中文字体

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
29
30
31
32
33
34
35
36
37
38
[root@iZ2ze49kwoa6pxxyyxdoa7Z win]# fc-list :lang=zh
/usr/share/fonts/win/msjh.ttc: Microsoft JhengHei UI:style=Regular
/usr/share/fonts/win/STXIHEI.TTF: STXihei:style=Regular
/usr/share/fonts/win/STZHONGS.TTF: STZhongsong:style=Regular
/usr/share/fonts/win/STKAITI.TTF: STKaiti:style=Regular
/usr/share/fonts/win/simkai.ttf: KaiTi:style=Regular,Normaali
/usr/share/fonts/win/STFANGSO.TTF: STFangsong:style=Regular
/usr/share/fonts/win/msyhbd.ttc: Microsoft YaHei UI:style=Έντονα
/usr/share/fonts/win/msyhl.ttc: Microsoft YaHei UI,Microsoft YaHei UI Light:style=Light,Regular
/usr/share/fonts/win/msyhbd.ttc: Microsoft YaHei:style=Έντονα
/usr/share/fonts/win/msyh.ttc: Microsoft YaHei UI:style=Normal
/usr/share/fonts/win/STXINWEI.TTF: STXinwei:style=Regular
/usr/share/fonts/win/Dengl.ttf: DengXian,DengXian Light:style=Light,Regular
/usr/share/fonts/win/hpsimplifiedhans-light.ttf: HP Simplified Hans Light,HP Simplified Hans:style=Regular,Light
/usr/share/fonts/win/FZYTK.TTF: FZYaoTi:style=Regular
/usr/share/fonts/win/msjhbd.ttc: Microsoft JhengHei:style=Félkövér
/usr/share/fonts/win/msyh.ttc: Microsoft YaHei:style=Normal
/usr/share/fonts/win/msyhl.ttc: Microsoft YaHei,Microsoft YaHei Light:style=Light,Regular
/usr/share/fonts/win/simsun.ttc: SimSun,宋体:style=Regular,常规
/usr/share/fonts/win/SIMLI.TTF: LiSu:style=Regular
/usr/share/fonts/win/STXINGKA.TTF: STXingkai:style=Regular
/usr/share/fonts/win/Deng.ttf: DengXian:style=Regular
/usr/share/fonts/win/SIMYOU.TTF: YouYuan:style=Regular
/usr/share/fonts/win/Dengb.ttf: DengXian:style=Bold
/usr/share/fonts/win/FZSTK.TTF: FZShuTi:style=Regular
/usr/share/fonts/win/msjh.ttc: Microsoft JhengHei:style=Regular
/usr/share/fonts/win/STSONG.TTF: STSong:style=Regular
/usr/share/fonts/win/msjhbd.ttc: Microsoft JhengHei UI:style=Félkövér
/usr/share/fonts/win/simsun.ttc: NSimSun,新宋体:style=Regular,常规
/usr/share/fonts/win/simfang.ttf: FangSong:style=Regular,Normaali
/usr/share/fonts/win/simhei.ttf: SimHei:style=Normal
/usr/share/fonts/win/STHUPO.TTF: STHupo:style=Regular
/usr/share/fonts/win/方正粗黑宋简体.ttf: FZCuHeiSongS\-B\-GB:style=Regular
/usr/share/fonts/win/msjhl.ttc: Microsoft JhengHei UI,Microsoft JhengHei UI Light:style=Light,Regular
/usr/share/fonts/win/hpsimplifiedhans-regular.ttf: HP Simplified Hans:style=Regular
/usr/share/fonts/win/STCAIYUN.TTF: STCaiyun:style=Regular
/usr/share/fonts/win/msjhl.ttc: Microsoft JhengHei,微軟正黑體 Light:style=Light,Regular
/usr/share/fonts/win/STLITI.TTF: STLiti:style=Regular

到这里字体就安装完毕了,再试试生成PDF,没有问题了

01