Home > database >  DRF generate html to pdf
DRF generate html to pdf

Time:12-09

Im trying to generate pdf from given html file but get_template function is not working i guess.

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)

    

The Except block returns None.

CodePudding user response:

Change line to:

pdf = pisa.pisaDocument(BytesIO(html.encode("utf-8")), result)

CodePudding user response:

It was my logical error issue. I didn't added 'templates' folder to DIRS in settings.py file.

  • Related