D
D
devrais2019-04-08 13:13:26
Django
devrais, 2019-04-08 13:13:26

Doesn't see Cyrillic xhtml2pdf django?

Conclusion

The Covenant 6
Soul: asdasdas asdasdasd
Blood-type : ываываыва

utils.py
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template

from xhtml2pdf import pisa

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 render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result, encoding='utf-8',
                                                           link_callback=fetch_pdf_resources)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None

pdf.html
{% load static %}
<!DOCTYPE>
<html>
<head>
  <title>NEW TITLE</title>
    <style>
    @font-face {
      font-family: Arial;
      src: url("{% static 'ttf/arial.ttf' %}");
    }

    body {
      font-family: Arial;
      font-size: 35px;
    }
</style>

</head>
<body>
  <p class='title'>The Covenant {{ number }} </p>

      Soul: {{ name_f }} {{ name_l }} <br/>
      Blood-type : {{ blood_type }}

</body>
</html>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question