DEV Community

aakas
aakas

Posted on • Edited 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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay