DEV Community

aakas
aakas

Posted on • Updated on

how to generate pdf in django in other languages?

I use xhtml2pdf to generate pdf in django.
The pdf generation smooth for the english language. But i have to generate pdf in nepali language, how can i do it?

from django.template.loader import get_template
from xhtml2pdf import pisa

def generate_pdf(request):
    template = get_template('pdf_template.html')
    html = template.render({'title':'abc', 'data':'नमस्कार'})
    result = BytesIO()

    try:
        pdf = pisa.pisaDocument(BytesIO(html.encode('utf-8')), result)
    except Exception as e:
        return HttpResponse(e)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type="application/pdf")
    return None
Enter fullscreen mode Exit fullscreen mode

The issue here is "UnicodeEncodeError: 'latin-1' codec can't encode character".

Top comments (0)