簡體   English   中英

如何在Python中使用Reportlab和Pisa從HTML文件生成PDF文件?

[英]How to generate PDF file from an HTML file using Reportlab and Pisa in Python?

進行以下代碼設置,以使用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!

這是點凍結產生的“部分列表”。

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的任何其他方法(方法和/或不同的庫)?

得到了它的支持...卸載並重新安裝了比薩,它就可以了! :)

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM