Как преобразовать файл отчета HTML в PDF, используя Python? - PullRequest
0 голосов
/ 21 апреля 2020

Ниже код успешно выполняется, когда я передаю сценарий HTML в «static_report». Как я могу передать имя файла или URL в «static_report»?

#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')

1 Ответ

0 голосов
/ 21 апреля 2020

Используя библиотеку pdfkit, вы можете конвертировать HTML в pdf. HTML может быть локальной страницей или URL-адресом, указанным в этой ссылке.

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

...