最近要使用到 HTML 转换成 PDF 的功能,想在在 .net 下做开发,后来发现真的很不方便,组件基本都没人维护。
于是转到了 python 下,朋友疯介绍 wkhtmltopdf,测试了一番,果真方便明了。
但是在安装库的时候稍微有点碰壁,这里就记一下。
一开始偷懒,使用 yum 直接安装。结果运行的时候发生错误
IOError: wkhtmltopdf: cannot connect to X server
查了下,知道了 yum 下的 wkhtmltopdf 库版本比较老。于是去官网找了一下,原来是想找源码来编译的,结果发现 rpm 包,又可以偷懒了。
[root@VM_224_197_centos pdf.azpro.cn]# rpm -ivh wkhtmltox-0.12.5-1.centos7.x86_64.rpm
error: Failed dependencies:
xorg-x11-fonts-75dpi is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
xorg-x11-fonts-Type1 is needed by wkhtmltox-1:0.12.5-1.centos7.x86_64
砰....撞墙了。安装时候发现需要 2 个库的支持。于是用 yum 安装了吧
[root@VM_224_197_centos pdf.azpro.cn]# yum install xorg-x11-fonts-75dpi
[root@VM_224_197_centos pdf.azpro.cn]# yum install xorg-x11-fonts-Type1
安装结束后,顺利安装上了 wkhtmltopdf ,至此,运行基本正常。
但是打开生成的pdf,结果发现没有中文。
初步怀疑没有安装中文字体,于是找到了simsun.ttc
,直接复制到 /usr/share/fonts/chinese/TrueType/
目录,在运行,效果完美。
参考文献: