首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用python3将HTML转换为PDF

如何用python3将HTML转换为PDF
EN

Stack Overflow用户
提问于 2015-06-03 00:16:01
回答 2查看 7.9K关注 0票数 5

如何用python3转换HTML到PDF?我用pyqt5写了一些关于webView的代码,我想把webView中的html转换成pdf,我该怎么办?

我尝试使用html2pdf,但它似乎只支持python2.x,并且我尝试安装wkhtmltox-0.12.2.2_msvc2013-win64.exe和pdfkit,然后使用示例代码。

代码语言:javascript
复制
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错误正在跟随。

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 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')

它真的很简单。

票数 4
EN

Stack Overflow用户

发布于 2017-07-05 09:29:38

'set path'可能意味着将**.exe添加到系统环境变量$Path$中。例如,将D:\Program Files\wkhtmltopdf\bin添加到$Path$

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30601322

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档