dimanche 27 décembre 2020

PDF document does not contain utf-8 encoding

I am making an application on Django. You need to add html page to PDF document. The document is being created, but the Cyrillic alphabet is smeared with squares.

#views.py
def fetch_pdf_resources(uri, rel):
    if uri.find(settings.MEDIA_URL) != -1:
        path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ''))
    elif uri.find(settings.STATIC_URL) != -1:
        path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ''))
    else:
        path = None
    return path


def export_pdf(request, pk):
    obj = get_object_or_404(Application, pk=pk)
    template_path = 'applications/applications_detail.html'
    context = {'applications': obj, 'to_pdf': True}
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="ApplicationDetail.pdf"'

    template = get_template(template_path)
    html = template.render(context)

    pisa_status = pisa.CreatePDF(BytesIO(html.encode('UTF-8')), response, encoding='utf-8', link_callback=fetch_pdf_resources)
    return response


#urls.py
    path('export_pdf/', views.export_pdf, name = 'export_pdf')



Aucun commentaire:

Enregistrer un commentaire