0

具有以下代码设置,以在 Python 中使用 Reportlab 和 Pisa 生成 PDF 文档。

import cStringIO
import ho.pisa as pisa

def html_to_pdf(data, filename, open=False):
    pdf = pisa.CreatePDF(
        cStringIO.StringIO(data),
        file(filename, "wb"))

我的 HTML 文件包含标准的 HTML 内容。

它的完全限定路径以及 .html 扩展名被分配给 output_file 变量。

像这样称呼它:

with open(output_file, "r") as my_file:
        contents = my_file.read()

html_to_pdf(contents, dest_pdf_file, open=True)

得到这个错误:

No handlers could be found for logger "sx.pisa3"
Traceback (most recent call last):
  File "/home/devuser/myapp/app.py", line 8, in <module>
    from utils.fileutils import FileUtil
  File "/home/devuser/myapp/utils/fileutils.py", line 5, in <module>
    import ho.pisa as pisa
  File "/usr/local/lib/python2.7/dist-packages/pisa-3.0.33-py2.7.egg/ho/pisa/__init__.py", line 26, in <module>
    from sx.pisa3.pisa import *
  File "/usr/local/lib/python2.7/dist-packages/pisa-3.0.33-py2.7.egg/sx/pisa3/__init__.py", line 41, in <module>
    from pisa import *
  File "/usr/local/lib/python2.7/dist-packages/pisa-3.0.33-py2.7.egg/sx/pisa3/pisa.py", line 32, in <module>
    from pisa_document import *
  File "/usr/local/lib/python2.7/dist-packages/pisa-3.0.33-py2.7.egg/sx/pisa3/pisa_document.py", line 22, in <module>
    from pisa_context import pisaContext
  File "/usr/local/lib/python2.7/dist-packages/pisa-3.0.33-py2.7.egg/sx/pisa3/pisa_context.py", line 21, in <module>
    from pisa_util import *
  File "/usr/local/lib/python2.7/dist-packages/pisa-3.0.33-py2.7.egg/sx/pisa3/pisa_util.py", line 55, in <module>
    raise ImportError("Reportlab Version 2.1+ is needed!")
ImportError: Reportlab Version 2.1+ is needed!

这是 pip freeze 产生的“部分列表”。

Pillow==2.3.0
PyPDF2==1.25.1
html5lib==0.999
oneconf==0.3.7.14.04.1
pdfkit==0.5.0
pisa==3.0.33
reportlab==3.0

似乎是一个损坏的安装问题...

有谁知道如何修复这个或任何用于将 HTML 文件生成为 PDF 的替代方法(方法和/或不同的库)?

4

1 回答 1

0

得到它的工作......卸载并重新安装比萨,它工作!:)

sudo easy_install pisa

我的代码:

import cStringIO
import ho.pisa as pisa

class FileUtil:
    @staticmethod
    def html_to_pdf(html, output_file):
        pdfFile = file(output_file, "wb")
        pdf = pisa.CreatePDF(
            cStringIO.StringIO(html.encode("ISO-8859-1")), pdfFile)
        pdfFile.close()
于 2015-11-25T00:33:56.463 回答