Linux 添加中文字体库

背景:润乾报表 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、建立字体缓存:  

root@gxx:/usr/share/fonts# mkfontscale
root@gxx:/usr/share/fonts# mkfontdir
root@gxx:/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。

root@gxx:~# 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

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:要给Python安装中文字体,可以按照以下步骤进行: 1. 首先需要安装字体工具fonttools。可以使用pip包管理器直接安装: ``` pip install fonttools ``` 2. 下载中文字体文件,将其转换为Python可以使用的格式,通常是TTF或OTF格式。可以从以下网站下载中文字体文件: - https://www.fontsquirrel.com/fonts/list/language/chinese - https://www.google.com/get/noto/#sans-hans 3. 使用fonttools的命令行工具ttx将字体文件转换为XML格式。在命令行终端里使用以下命令: ``` ttx font_file.ttf ``` 4. 打开生成的XML文件,找到<namerecord>标签,查看有没有“full_name”属性值是中文字体的全名。如果没有,则需要添加一个,例如: ``` <namerecord nameID="4" platformID="3" platEncID="1" langID="0x409">My Chinese Font</namerecord> ``` 5. 将XML文件保存为新的字体文件,例如: ``` ttx new_font_file.ttx ``` 6. 将新的字体文件复制到Python的字体目录下。可以使用以下代码查看Python的字体目录: ```python import matplotlib.font_manager as font_manager font_manager.findSystemFonts(fontpaths=None, fontext='ttf') ``` 通常,Windows系统的字体目录是C:\Windows\Fonts,Linux系统的字体目录是/usr/share/fonts或~/.fonts。 7. 设置Python中文字体的默认值。可以使用以下代码设置所有绘图的中文字体为新安装的字体: ```python import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['My Chinese Font'] ``` 以上就是给Python安装中文字体的全部步骤了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值