【发布时间】: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')
【问题讨论】:
-
似乎您需要将文件作为字符串读取。 stackoverflow.com/questions/8369219/…
-
或将 url 读取为字符串。 stackoverflow.com/questions/15138614/…
-
欢迎来到 SO!你试过什么?您遇到的具体错误是什么?
标签: python python-3.x python-2.7 wkhtmltopdf xhtml2pdf