Python3 html 转 pdf

Mik*_*ike 3 html pdf python-3.x

如何在Python3中将HTML转换为PDF?Xhtml2pdf 在 Python3 中不起作用,出现错误:

导入 xhtml2pdf.pisa 作为 pisa Traceback(最近一次调用):文件“”,第 1 行,在文件“/home/hound/test/python/test_env/lib/python3.4/site-packages/xhtml2pdf/ init .py ”,第 41 行,来自 xhtml2pdf.util import REPORTLAB22 文件“/home/hound/test/python/test_env/lib/python3.4/site-packages/xhtml2pdf/util.py”,第 302 行引发异常,“框不定义正确的方式” ^ SyntaxError:语法无效

jam*_*ell 5

到目前为止我发现的最好的是weasyprint.

文档中:

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

它真的很容易工作。为我节省了大量时间(在我浪费时间试图让 xhtml2pdf 和其他人在 python 3 中工作但失败之后。

  • 关于 weasyprint 的快速说明:我遇到了表格单元格中长字符串重叠的问题。找不到正确的 css 属性来将其设置为换行而不是截断。事实证明,正确的答案是“overflow-wrap:break-word;”,并且还设置了“width:x;”。 (2认同)