最近在Centos7 服务器上运行Flask应用,用到了把html转成pdf的功能,便使用了wkhtmltopdf库。但在部署上服务器后出现了无法运行该库,报错wkhtmltopdf: cannot connect to X server
,并且生成的pdf文字为方块的问题。
问题出在CentOS的yum库里的wkhtmltopdf版本过旧,而新版的不再依赖X server。于是卸载旧wkhtmltopdf再从官网下载预编译版的。需要先安装yum库里的wkhtmltopdf。
wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
tar xvfJ wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
cd wkhtmltox/bin
sudo mv ./wkhtmltopdf /usr/bin/wkhtmltopdf
sudo chmod +x /usr/bin/wkhtmltopdf
(optional) 复制一份到/usr/local/bin
sudo cp /usr/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
运行命令测试:
wkhtmltopdf google.com ~/test.pdf
如果运行没问题且生成PDF正常,可以到此为止。
如果生成的字符都是方块,那么安装standard PostScript fonts:
sudo yum install urw-fonts libXext openssl-devel
收工。
参考资料:
https://github.com/pdfkit/pdfkit/issues/180
http://www.cnblogs.com/Jerryshome/p/5151664.html