欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux 添加中文字体库

程序员文章站 2022-03-21 11:05:13
...

背景:润乾报表 win上面的项目直接上传到linux 上,但预览&下载报表时乱码

如果含有斜线的单元格或统计图的报表在网页上发布时,统计图或斜线单元格里的汉字会变成小方框,此时往往是服务器端操作系统的中文安装包没有装全,导致报表里用到的字体在服务器端的操作系统里找不到,因此成了小方块。

我这只配置到前 5 步就可以了,不需要重启Linux 或字体服务,步骤总结为以下:

1、检查字体是否已经安装:  

fc-list  ----------检查所有已有的字体

fc-list :lang=zh -----------只检查中文

2、到 C:\windows\fonts 复制对应字体库,微软雅黑、宋体、黑体等,各文件后缀可能不一样,有的为ttf,有的为ttc,不影响使用。

3、上传刚才复制的字体库到/usr/share/fonts/zh_CN目录下,如果没有该目录,用命令:

mkdir /usr/share/fonts/zh_CN

  来创建,然后再上传。

4、修改字体权限,使root以外的用户可以使用这些字体:使用777 赋予全部权限

chmod -R 777 /usr/share/fonts/zh_CN

5、建立字体缓存:  

[email protected]:/usr/share/fonts# mkfontscale
[email protected]:/usr/share/fonts# mkfontdir
[email protected]:/usr/share/fonts# fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 4 dirs
/usr/share/fonts/X11: caching, new cache contents: 0 fonts, 2 dirs
/usr/share/fonts/X11/encodings: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/X11/encodings/large: caching, new cache contents: 0 fonts, 0 dirs
/usr/share/fonts/X11/util: caching, new cache contents: 0 fonts, 0 dirs
/usr/share/fonts/cmap: caching, new cache contents: 0 fonts, 5 dirs
/usr/share/fonts/cmap/adobe-cns1: caching, new cache contents: 0 fonts, 0 dirs
/usr/share/fonts/cmap/adobe-gb1: caching, new cache contents: 0 fonts, 0 dirs
/usr/share/fonts/cmap/adobe-japan1: caching, new cache contents: 0 fonts, 0 dirs
/usr/share/fonts/cmap/adobe-japan2: caching, new cache contents: 0 fonts, 0 dirs
/usr/share/fonts/cmap/adobe-korea1: caching, new cache contents: 0 fonts, 0 dirs
/usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/truetype/dejavu: caching, new cache contents: 21 fonts, 0 dirs
/usr/share/fonts/zh_CN: caching, new cache contents: 419 fonts, 0 dirs
/usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
Re-scanning /usr/share/fonts: caching, new cache contents: 0 fonts, 4 dirs
Re-scanning /usr/share/fonts/X11: caching, new cache contents: 0 fonts, 2 dirs
Re-scanning /usr/share/fonts/X11/encodings: caching, new cache contents: 0 fonts, 1 dirs
Re-scanning /usr/share/fonts/cmap: caching, new cache contents: 0 fonts, 5 dirs
Re-scanning /usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 1 dirs
/var/cache/fontconfig: cleaning cache directory
/root/.cache/fontconfig: not cleaning non-existent cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

6、重启 Tomcat,以上文章中有的说要重启 Linux系统,有的说要重启字体库服务,但我都没有,而且也没有字体服务,只是重启了Tomcat 执行后有效。

7、可以再次使用 fc-list 查看字体库,已经包含了新加入的字体,或者使用上边的Java 输出也包含了新字体,打印中文水印也OK。

[email protected]:~# fc-list :lang=zh
/usr/share/fonts/zh_CN/msyhl.ttc: Microsoft YaHei UI,Microsoft YaHei UI Light:style=Light,Regular
/usr/share/fonts/zh_CN/Dengl.ttf: DengXian,等线,DengXian Light,等线 Light:style=Light,Regular
/usr/share/fonts/zh_CN/msyh.ttc: Microsoft YaHei UI:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/zh_CN/simsun.ttc: SimSun,宋体:style=Regular,常规
/usr/share/fonts/zh_CN/Dengb.ttf: DengXian,等线:style=Bold
/usr/share/fonts/zh_CN/SIMLI.TTF: LiSu,隶书:style=Regular
/usr/share/fonts/zh_CN/Deng.ttf: DengXian,等线:style=Regular
/usr/share/fonts/zh_CN/STXINWEI.TTF: STXinwei,华文新魏:style=Regular
/usr/share/fonts/zh_CN/STHUPO.TTF: STHupo,华文琥珀:style=Regular
/usr/share/fonts/zh_CN/msjhl.ttc: Microsoft JhengHei,微軟正黑體,微軟正黑體 Light,Microsoft JhengHei Light:style=Light,Regular
/usr/share/fonts/zh_CN/simkai.ttf: KaiTi,楷体:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/zh_CN/STSONG.TTF: STSong,华文宋体:style=Regular
/usr/share/fonts/zh_CN/STFANGSO.TTF: STFangsong,华文仿宋:style=Regular
/usr/share/fonts/zh_CN/simhei.ttf: SimHei,黑体:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/zh_CN/FZYTK.TTF: FZYaoTi,方正姚体:style=Regular
/usr/share/fonts/zh_CN/FZSTK.TTF: FZShuTi,方正舒体:style=Regular
/usr/share/fonts/zh_CN/msjh.ttc: Microsoft JhengHei,微軟正黑體:style=Normal,Regular,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/zh_CN/STXINGKA.TTF: STXingkai,华文行楷:style=Regular
/usr/share/fonts/zh_CN/msyh.ttc: Microsoft YaHei,微软雅黑:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/zh_CN/msjhl.ttc: Microsoft JhengHei UI,Microsoft JhengHei UI Light:style=Light,Regular
/usr/share/fonts/zh_CN/simsun.ttc: NSimSun,新宋体:style=Regular,常规
/usr/share/fonts/zh_CN/STLITI.TTF: STLiti,华文隶书:style=Regular
/usr/share/fonts/zh_CN/STZHONGS.TTF: STZhongsong,华文中宋:style=Regular
/usr/share/fonts/zh_CN/STKAITI.TTF: STKaiti,华文楷体:style=Regular
/usr/share/fonts/zh_CN/msjhbd.ttc: Microsoft JhengHei,微軟正黑體:style=Negreta,Bold,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/usr/share/fonts/zh_CN/msjh.ttc: Microsoft JhengHei UI:style=Normal,Regular,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/zh_CN/msyhbd.ttc: Microsoft YaHei,微软雅黑:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/usr/share/fonts/zh_CN/msyhl.ttc: Microsoft YaHei,微软雅黑,Microsoft YaHei Light,微软雅黑 Light:style=Light,Regular
/usr/share/fonts/zh_CN/msjhbd.ttc: Microsoft JhengHei UI:style=Negreta,Bold,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/usr/share/fonts/zh_CN/STXIHEI.TTF: STXihei,华文细黑:style=Regular
/usr/share/fonts/zh_CN/STCAIYUN.TTF: STCaiyun,华文彩云:style=Regular
/usr/share/fonts/zh_CN/SIMYOU.TTF: YouYuan,幼圆:style=Regular
/usr/share/fonts/zh_CN/simfang.ttf: FangSong,仿宋:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/zh_CN/msyhbd.ttc: Microsoft YaHei UI:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia