DRF генерирует html в pdf

Я пытаюсь сгенерировать pdf из заданного html файла, но get_template функция не работает, я думаю.

from io import BytesIO
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(context_dict={}):
    try:
        template = get_template('invoice.html')
        html  = template.render(context_dict)
        result = BytesIO()
        pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
        if not pdf.err:
            return result.getvalue()
        return None
    except Exception as e:
        print('ERROR', e)

    

Блок Except возвращает None.

Вернуться на верх