how to convert HTML to PDF in Python3? Xhtml2pdf does not work in Python3, got error:
import xhtml2pdf.pisa as pisa Traceback (most recent call last): File "", line 1, in File "/home/hound/test/python/test_env/lib/python3.4/site-packages/xhtml2pdf/init.py", line 41, in from xhtml2pdf.util import REPORTLAB22 File "/home/hound/test/python/test_env/lib/python3.4/site-packages/xhtml2pdf/util.py", line 302 raise Exception, "box not defined right way" ^ SyntaxError: invalid syntax
The best that I found by far is
weasyprint
.From the documentation:
from weasyprint import HTML HTML('http://weasyprint.org/').write_pdf('/tmp/weasyprint-website.pdf')
and it really works that easy. Saved me tons of time (after I wasted time trying to get xhtml2pdf and others to work in python 3 but failed.
I had the same error. Apparently for now xhtml2pdf has Python3 support only in its prerelease version - 0.2b1 (for more info see https://pypi.python.org/pypi/xhtml2pdf). I’ve solved the problem by uninstalling the previous xhtml2pdf version and installing the prerelease version