在Centos7.5上安装wkhtmltopdf、中文字体及相关配置

在centos7.5上安装wkhtmltopdf

rpm -ivh /home/zjb/wkhtmltox-0.12.6-1.centos7.x86_64.rpm

根据提示安装缺失依赖

yum install -y libX11 libXext libXrender libjpeg xorg-x11-fonts-75dpi xorg-x11-fonts-Type1
Linux提示缺失依赖

确认安装的中文字体

上传字体文件到/usr/share/fonts/chinese,
然后执行字体缓存更新

fc-cache -fv

检测linux上安装的中文字体

fc-list :lang=zh-cn

[root@webadmin01 chinese]# fc-list :lang=zh-cn
/usr/share/fonts/chinese/youyuan.TTF: YouYuan:style=Regular
/usr/share/fonts/chinese/Songti.ttc: STSong:style=Regular,Normaali
/usr/share/fonts/chinese/Songti.ttc: Songti TC,宋体\-繁,宋體\-繁:style=Regular,常规体,標準體
/usr/share/fonts/chinese/Songti.ttc: Songti SC,宋体\-简,宋體\-簡:style=Regular,常规体,標準體
/usr/share/fonts/chinese/MSYH.TTC: Microsoft YaHei:style=Normal
/usr/share/fonts/chinese/SIMSUN.TTC: SimSun,宋体:style=Regular,常规
/usr/share/fonts/chinese/Songti.ttc: Songti SC,宋体\-简,宋體\-簡:style=Light,细体,細體
/usr/share/fonts/chinese/MSYHBD.TTC: Microsoft YaHei:style=Έντονα
/usr/share/fonts/chinese/STKAITI.TTF: STKaiti:style=Regular
/usr/share/fonts/chinese/Songti.ttc: Songti TC,宋体\-繁,宋體\-繁:style=Light,细体,細體
/usr/share/fonts/chinese/MSYH.TTC: Microsoft YaHei UI:style=Normal
/usr/share/fonts/chinese/隶书.ttf: LiSu:style=Regular
/usr/share/fonts/chinese/MSYHBD.TTC: Microsoft YaHei UI:style=Έντονα
/usr/share/fonts/chinese/STFANGSO.TTF: STFangsong:style=Regular
/usr/share/fonts/chinese/SIMSUN.TTC: NSimSun,新宋体:style=Regular,常规
/usr/share/fonts/chinese/MSYHL.TTC: Microsoft YaHei UI,Microsoft YaHei UI Light:style=Light,Regular
/usr/share/fonts/chinese/Songti.ttc: Songti SC:style=Black
/usr/share/fonts/chinese/SIMKAI.TTF: KaiTi:style=Regular,Normaali
/usr/share/fonts/chinese/Songti.ttc: Songti SC,宋体\-简,宋體\-簡:style=Bold,粗体,粗體
/usr/share/fonts/chinese/Songti.ttc: Songti TC,宋体\-繁,宋體\-繁:style=Bold,粗体,粗體
/usr/share/fonts/chinese/STSONG.TTF: STSong:style=Regular
/usr/share/fonts/chinese/仿宋_GB2312.ttf: FangSong_GB2312:style=Regular
/usr/share/fonts/chinese/SIMHEI.TTF: SimHei:style=Normal
/usr/share/fonts/chinese/楷体_GB2312.ttf: KaiTi_GB2312:style=Regular
/usr/share/fonts/chinese/SIMFANG.TTF: FangSong:style=Regular,Normaali
/usr/share/fonts/chinese/MSYHL.TTC: Microsoft YaHei,Microsoft YaHei Light:style=Light,Regular

检查能否解析中文字体名

for family in 微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆; do   echo -n "$family: ";   fc-match "$family"; done
[root@webadmin01 chinese]# for family in 微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆; do   echo -n "$family: ";   fc-match "$family"; done
微软雅黑: DejaVuSans.ttf: "DejaVu Sans" "Book"
宋体: SIMSUN.TTC: "SimSun" "Regular"
黑体: DejaVuSans.ttf: "DejaVu Sans" "Book"
仿宋: DejaVuSans.ttf: "DejaVu Sans" "Book"
仿宋_GB2312: DejaVuSans.ttf: "DejaVu Sans" "Book"
楷体: DejaVuSans.ttf: "DejaVu Sans" "Book"
楷体_GB2312: DejaVuSans.ttf: "DejaVu Sans" "Book"
隶书: DejaVuSans.ttf: "DejaVu Sans" "Book"
幼圆: DejaVuSans.ttf: "DejaVu Sans" "Book"

发现不能正常解析中文字体名

配置别名

vim /etc/fonts/fonts.conf
在<fontconfig></fontconfig>配置节之间增加以下match标签,增加中文字体解析规则。

  <!-- 为微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆;配置别名 -->
  <match>
    <test name="family"><string>微软雅黑</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>Microsoft YaHei</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>宋体</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>SimSun</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>黑体</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>SimHei</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>仿宋</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>FangSong</string>
    </edit>
  </match>
   <match>
    <test name="family"><string>仿宋_GB2312</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>FangSong_GB2312</string>
    </edit>
  </match>
   <match>
    <test name="family"><string>楷体</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>KaiTi</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>楷体_GB2312</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>KaiTi_GB2312</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>隶书</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>LiSu</string>
    </edit>
  </match>
  <match>
    <test name="family"><string>幼圆</string></test>
    <edit name="family" mode="assign" binding="strong">
      <string>YouYuan</string>
    </edit>
  </match>

配置后,再次检查解析情况

[root@webadmin01 chinese]# for family in 微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆; do   echo -n "$family: ";   fc-match "$family"; done
微软雅黑: MSYH.TTC: "Microsoft YaHei" "Normal"
宋体: SIMSUN.TTC: "SimSun" "Regular"
黑体: SIMHEI.TTF: "SimHei" "Normal"
仿宋: SIMFANG.TTF: "FangSong" "Regular"
仿宋_GB2312: 仿宋_GB2312.ttf: "FangSong_GB2312" "Regular"
楷体: SIMKAI.TTF: "KaiTi" "Regular"
楷体_GB2312: 楷体_GB2312.ttf: "KaiTi_GB2312" "Regular"
隶书: 隶书.ttf: "LiSu" "Regular"
幼圆: youyuan.TTF: "YouYuan" "Regular"

总结

  1. 在网页编写时,css font-family还是尽量用英文字体名称,不要使用中文字体名称。比如这样写:
font-family:Microsoft YaHei,tahoma,arial,Hiragino Sans GB,"\5B8B\4F53",sans-serif
  1. 在Linux环境,可以通过修改/etc/fonts/fonts.config来配置中文字体解析规则, 比如上面提到的适配“微软雅黑 宋体 黑体 仿宋 仿宋_GB2312 楷体 楷体_GB2312 隶书 幼圆”等这些中文字体名称。
 .gz-page-content .content-title .notes {
      font-size: 24px;
      padding: 14px 0 9px;
      margin-bottom: 30px;
      line-height: 150%;
      font-family: 'FangSong','仿宋', 'SimSun', '宋体';
      color: #333333;
    }

参考资料

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容