如何用python3转换HTML到PDF?我用pyqt5写了一些关于webView的代码,我想把webView中的html转换成pdf,我该怎么办?
我尝试使用html2pdf,但它似乎只支持python2.x,并且我尝试安装wkhtmltox-0.12.2.2_msvc2013-win64.exe和pdfkit,然后使用示例代码。
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')但我也failed.and错误正在跟随。
Traceback (most recent call last):
File "E:\Python34\lib\site-packages\pdfkit\configuration.py", line 21, in __init__
with open(self.wkhtmltopdf) as f:
FileNotFoundError: [Errno 2] No such file or directory: b''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pdfkit.from_url('http://google.com', 'out.pdf')
File "E:\Python34\lib\site-packages\pdfkit\api.py", line 22, in from_url
configuration=configuration)
File "E:\Python34\lib\site-packages\pdfkit\pdfkit.py", line 38, in __init__
self.configuration = (Configuration() if configuration is None
File "E:\Python34\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
OSError: No wkhtmltopdf executable found: "b''"
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但我也failed.so我应该做什么?我的系统是window7 x64,python的版本是3.4
发布于 2017-10-12 21:17:07
由于这还没有一个公认的答案,有一个很棒的库可以在Python3中工作,我在多次搜索和尝试使用PyPDF2、Python3的wkhtmltopdf beta分支、qpdf等之后发现了这个库。我有相关的答案和示例代码here。
出于完整性考虑,在documentation中:
from weasyprint import HTML HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf')
它真的很简单。
发布于 2017-07-05 09:29:38
'set path'可能意味着将**.exe添加到系统环境变量$Path$中。例如,将D:\Program Files\wkhtmltopdf\bin添加到$Path$。
https://stackoverflow.com/questions/30601322
复制相似问题