My Tamil letters are not displaying in Pdf file in Django

304 Views Asked by At
views.py

def pdf(request,songsheetname):
    username=request.user.username
    printsong=Songsprintform.objects.all().filter(username=username,removesong='0',
     addsheetstatus='0',songsheetname=songsheetname,songprintstatus='1')
     countsong=Songsprintform.objects.all().filter(username=username,removesong='0',
     addsheetstatus='0',songsheetname=songsheetname,songprintstatus='1').count()
    songid = []
    for i in printsong:
        songid.append(i.songprintid)
    recentsongs=SongList.objects.all().filter(id__in=songid)
    template_path = 'pdf.html'
    context = {'recentsongs':recentsongs}
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename="songs.pdf"'
    template = get_template(template_path)
    html = template.render(context)
    pisa_status = pisa.CreatePDF(html, dest=response)
    if pisa_status.err:
       return HttpResponse('We had some errors <pre>' + html + '</pre>')
    return response


django template

<html>
<head>
<title>Page Title</title>
<meta charset="utf-8">
<style>
div {
    column-count: 2;
    column-width: 300px;
}
</style>
</head>
<body>
<div class="col-md-6">
{% for i in recentsongs %}
      <p class="float-left">{{i.body |linebreaks}}</p>
      {% endfor %}
</div>
</div>
</body>
</html>

This is my code... Here I'm converting my Django template(html) page into pdf. All are Working fine but here my content are in Tamil. But here it displays as an Square box instead of Tamil Letters .whenever my click my button on see that pdf file it always shown as an square box. I don't Know why.Please help me...

1

There are 1 best solutions below

3
Alon Alush On

Maybe the font you're using in the PDF does not support Tamil characters. Try changing to a font that supports them.

<html>
<head>
<style>
@font-face {
    font-family: 'TamilFont';
    src: url('/path/to/font.ttf') format('truetype');
}
body {
    font-family: 'TamilFont';
}
</style>
</head>
<body>
<div class="col-md-6">
{% for i in recentsongs %}
      <p class="float-left">{{i.body |linebreaks}}</p>
      {% endfor %}
</div>
</div>
</body>
</html>

In your django python file, embed a font that supports Tamil characters. This way, when you convert it, the PDF software will recognize the font you want to use.