devrais
@devrais

Не видит кириллицу xhtml2pdf django?

Вывод
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>
  • Вопрос задан
  • 883 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
23 апр. 2024, в 19:21
300 руб./за проект
23 апр. 2024, в 19:05
15000 руб./за проект
23 апр. 2024, в 19:01
7000 руб./за проект