Python3 html to pdf

2020-07-22 19:11发布

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

2条回答
乱世女痞
2楼-- · 2020-07-22 19:47

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.

查看更多
The star\"
3楼-- · 2020-07-22 20:07

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

pip install --pre xhtml2pdf
查看更多
登录 后发表回答