【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
1.安装pdfkit
pip3 install pdfkit
代码:
html = '<html><head><meta charset="UTF-8"></head>' \
'<body><div align="center"><p>%s</p></div></body></html>' % text
pdfkit.from_string(html, './result.pdf') 或者 pdfkit.from_file(1.html, './result.pdf')
2.测试运行
运行python3 test.py后报错
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
缺少:wkhtmltopdf 进行安装 yum install wkhtmltopdf
github地址:https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
官网:https://wkhtmltopdf.org/downloads.html
3.继续报错
OSError: wkhtmltopdf: cannot connect to X server
安装xvfb
yum install xorg-x11-server-Xvfb
在 /usr/bin/ 目录下生成脚本 wkhtmltopdf.sh 并写入命令
vim /usr/bin/wkhtmltopdf.sh
在.sh文件中写入:xvfb-run -a --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf -q $*
chmod a+x /usr/bin/wkhtmltopdf.sh
ln -s /usr/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf
启动Xserver :wkhtmltopdf http://www.baidu.com a.pdf
如果中午乱码或者报错format相关
参考:https://blog.csdn.net/q490880/article/details/81121246
来源:oschina
链接:https://my.oschina.net/songms/blog/3142224