【问题标题】:How to convert a HTML report file into a PDF using Python?如何使用 Python 将 HTML 报告文件转换为 PDF?
【发布时间】:2020-04-21 05:04:25
【问题描述】:

当我在“static_report”中传递 HTML 脚本时,以下代码运行成功。如何在“static_report”中传递文件名或 URL?

#Converting HTML To PDF
from xhtml2pdf import pisa

def convert_html_to_pdf(source_html, output_filename):
    # open output file for writing (truncated binary)
    result_file = open(output_filename, "w+b")

    # convert HTML to PDF
    pisa_status = pisa.CreatePDF(
            source_html,                # the HTML to convert
            dest=result_file)           # file handle to recieve result

    # close output file
    result_file.close()                 # close output file

    # return True on success and False on errors
    return pisa_status.err

static_report = ""
convert_html_to_pdf(static_report, 'report.pdf')

【问题讨论】:

标签: python python-3.x python-2.7 wkhtmltopdf xhtml2pdf


【解决方案1】:

使用 pdfkit 库,您可以将 HTML 转换为 pdf。 HTML 可以是此链接中给出的本地页面或 URL。

[https://www.geeksforgeeks.org/python-convert-html-pdf/][1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2021-06-16
    • 2012-05-01
    • 2016-03-10
    • 2019-02-10
    相关资源
    最近更新 更多