0

Below code is running successfully when I am passing HTML script in 'static_report'. How can I pass File name or URL in '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')
Hao Tan
  • 1,530
  • 2
  • 13
  • 20
  • 1
    seems like you need to read a file as a string. https://stackoverflow.com/questions/8369219/how-to-read-a-text-file-into-a-string-variable-and-strip-newlines – Hao Tan Apr 21 '20 at 05:21
  • or read a url as a string. https://stackoverflow.com/questions/15138614/how-can-i-read-the-contents-of-an-url-with-python – Hao Tan Apr 21 '20 at 05:22
  • Welcome to SO! What have you tried? What was exactly the error you got? – sophros Apr 21 '20 at 05:43

1 Answers1

0

Using the pdfkit library, you can convert HTML into pdf. HTML can be a local page or URL as given in this link.

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

Dinuka
  • 37
  • 1
  • 9