【解决】python pdfkit中文乱码,亲测可用

在使用pdfkit 将html转化为pdf文件是出现了中文乱码的问题,在网上找了一下原因有两个
第一个 服务器不支持中文
第二个 在html文档当中需要标明编码格式

第二种不难说一下第一个,我使用的系统是centos7.2,
葵花宝典
第一式:检查系统是否有字体库

查看字体库列表

 
 
  
  

fc-list

如果没有安装字体库,使用下面的命令安装字体库

 
 
  
  

yum -y install fontconfig

第二式:安装中文字体

首先在/usr/shared/fonts 目录下新建一个目录 chinese。

 
 
  
  

mkdir /usr/shared/fonts/chinese

上传字体文件到 chinese 文件夹中。

 
 
  
  

simsun.ttc
simsunb.ttf

修改 chinese 目录的权限

 
 
  
  

chmod -R 755 /usr/share/fonts/chinese

安装 ttmkfdir 来搜索目录中所有的字体信息,并汇总生成 fonts.scale 文件。

 
 
  
  

yum -y install ttmkfdir

ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

修改字体配置文件,添加 chinese 文件夹的地址

 
 
  
  

vi /etc/fonts/fonts.conf

<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>

<!-- 添加chinese文件夹的地址 -->
<dir>/usr/share/fonts/chinese</dir>

<dir prefix="xdg">fonts</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>

清除字体缓存

 
 
  
  

fc-cache

OKOK,接下来就是见证奇迹的时刻了。
感谢无名前辈留下的经验

Linux CentOS 7 安装字体库 & 中文字体:https://www.linuxidc.com/Linux/2016-09/135548.htm
centos系统怎么设置系统语言为中文:https://www.php.cn/centos/445626.html

 
 
  
  

猜你喜欢

转载自blog.csdn.net/ab6326795/article/details/131382809