How to convert HTML to PDF using python3

How to convert HTML to PDF using python3? I am writing code about webView with pyqt5 and I want to convert html to webView to pdf, what should I do?

I tried using html2pdf, but it seems to only support python2.x and I tried installing wkhtmltox-0.12.2.2_msvc2013-win64.exe and pdfkit, and then use the sample code.

import pdfkit pdfkit.from_url('http://google.com', 'out.pdf') pdfkit.from_file('test.html', 'out.pdf') pdfkit.from_string('Hello!', 'out.pdf') 

but I also failed. And the following error.

 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 

but I also failed. What should I do? My system is window7 x64, python version is 3.4

+5
source share
2 answers

'set path' possibly means **.exe adds the $Path$ system environment variable. For example, add D:\Program Files\wkhtmltopdf\bin to $Path$ .

+1
source

Since this has not yet accepted the answer, there is a large library that works in Python 3, which I found after many searches and unsuccessful attempts to use PyPDF2, the wkhtmltopdf beta branch for Python 3, qpdf, etc. This is a weasyprint. I have a corresponding answer and sample code here .

For completeness, from the documentation :

from weasyprint import HTML HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf')

and it really is that simple.

+1
source

All Articles