最近使用puppeteer來寫一個(gè)M頁的監(jiān)控工具,部署到linux上運(yùn)行后發(fā)現(xiàn)截圖中文亂碼了:

搜了一些資料,解決方法如下:
一、安裝fontconfig
yum -y install fontconfig
這個(gè)命令執(zhí)行完成之后,就可以在/usr/share文件夾里面看到fonts和fontconfig
二、添加中文字體庫(kù)
從window的C:\Windows\Fonts里面把你需要的字體拷貝出來。比如simfang.ttf
在CentOS的/usr/share/fonts新建一個(gè)叫chinese的文件夾
然后把剛剛拷貝字體放到CentOS的/usr/share/fonts/chinese里面
修改chinese目錄的權(quán)限:
chmod -R 775 /usr/share/fonts/chinese
接下來需要安裝ttmkfdir來搜索目錄中所有的字體信息,并匯總生成fonts.scale文件,輸入命令
yum -y install ttmkfdir
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
修改字體配置文件 vi /etc/fonts/fonts.conf
<!-- Font directory list -->
<dir>/usr/share/fonts</dir>
<dir>/usr/share/X11/fonts/Type1</dir>
<dir>/usr/share/X11/fonts/TTF</dir>
<dir>/usr/local/share/fonts</dir>
<dir>/usr/local/share/fonts/chinese</dir>
<dir prefix="xdg">fonts</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>
刷新內(nèi)存中的字體緩存,fc-cache
看一下現(xiàn)在機(jī)器上已經(jīng)有了剛才添加的字體。fc-list :lang=zh

現(xiàn)在再運(yùn)行一下截圖,就沒問題啦~
