expected string or bytes-like object, got 'NoneType' error on using xhtml2pdf module of python

297 Views Asked by At

I am facing the following error while rendering HTML to PDF by using xhtml2pdf of python.

expected string or bytes-like object, got 'NoneType'

Need a valid file name!

'<img src="assets/images/db.png"/>'

Need a valid file name!

'<img src="assets/images/visatrans.png"/>'

Need a valid file name!

'<img src="assets/images/db.png" alt=""/>'

Please check the following sample code.

import sys
from xhtml2pdf import pisa

OUTPUT_FILENAME = "test.pdf"
TEMPLATE_FILE = "templates/test.html"


def html_to_pdf(content, output):

    # Open file to write
    result_file = open(output, "w+b")

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

    # close output file
    result_file.close()

    result = pisa_status.err

    if not result:
        print("Successfully created PDF")
    else:
        print("Error: unable to create the PDF")

    # return False on success and True on errors
    return result


def from_template(template, output):
    # Reading our template
    source_html = open(template, "r")
    content = source_html.read()  # the HTML to convert
    source_html.close()  # close template file

    html_to_pdf(content, output)


from_template(TEMPLATE_FILE, OUTPUT_FILENAME)
0

There are 0 best solutions below