cenos7安装wkhtmltopdf

参考:http://www.023dns.com/server_ECS/2190.html
https://blog.csdn.net/nexttrial/article/details/125504683

在Centos7 安装wkhtmltopdf,把html页面转换成pdf文档。

1、安装

yum install wkhtmltopdf

2、安装之后,执行操作,若提示“wkhtmltopdf: cannot connect to X server”错误

3、需再安装xvfb

yum install xorg-x11-server-Xvfb

4、可使用以下命令,把html网页转换成pdf文档。

xvfb-run --server-args=“-screen 0, 1024x768x24” wkhtmltopdf http://www.aliyun.com aliyun.pdf

或把这个命令,写入到一个脚本中。

vim wkhtmltopdf.sh

写入以下命令到脚本 wkhtmltopdf.sh中。

xvfb-run --server-args=“-screen 0, 1024x768x24” wkhtmltopdf “$@”

设置脚本权限

chmod +x wkhtmltopdf.sh

直接用此脚本也可。

./wkhtmltopdf.sh www.baidu.com 2.pdf

wkhtmltopdf乱码解决方案

https://liumapp.gitee.io/liumapp/articles/2017/04/10/1491811668985.html/

查看linux上的字体:

fc-list

windows10 字体路径为: C:\Windows\Fonts ,在里面找到需要的字体,其中宋体 常规为:SIMSUN.TTC,将改字体复制到linux的/usr/share/fonts里面

原因:
wkhtmltopdf会使用x11的某些库对字体进行渲染,而通过php+http的方式调用命令,无法使用到这些库,就导致了乱码现象

解决方案:
将window中的simsun.ttc和simsun.ttc都复制到cenos7(linux)的/usr/share/fonts路径下,

vim /etc/fonts/fonts.conf

执行这条命令可以看到,linux会自动去fonts包下面去找字体文件到系统环境中
在这里插入图片描述
自己配的其他路径可以加 <dir>/usr/share/fonts/chinese</dir> 这样一行文件加路径
<!— 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 prefix="xdg">fonts</dir>
  <!— the following element will be removed in the future —>
  <dir>~/.fonts</dir>
  <dir>/usr/share/fonts/chinese</dir>
/usr/share/fonts

在这里插入图片描述
在这里插入图片描述
成功!

猜你喜欢

转载自blog.csdn.net/qq_43470725/article/details/130546366