xhtml2pdf кириллица

Генерирует кубики.

    def link_callback(uri, rel):

    Convert HTML URIs to absolute system paths so xhtml2pdf can access those
    resources
    
    result = finders.find(uri)
    if result:
        if not isinstance(result, (list, tuple)):
            result = [result]
        result = list(os.path.realpath(path) for path in result)
        path = result[0]
    else:
        sUrl = settings.STATIC_URL  # Typically /static/
        sRoot = settings.STATIC_ROOT  # Typically /home/userX/project_static/
        mUrl = settings.MEDIA_URL  # Typically /media/
        mRoot = settings.MEDIA_ROOT  # Typically /home/userX/project_static/media/
 
        if uri.startswith(mUrl):
            path = os.path.join(mRoot, uri.replace(mUrl, ""))
        elif uri.startswith(sUrl):
            path = os.path.join(sRoot, uri.replace(sUrl, ""))
        else:
            return uri
 
    # make sure that file exists
    if not os.path.isfile(path):
        raise Exception(
            'media URI must start with %s or %s' % (sUrl, mUrl)
        )
    return path
 
def render_pdf_new(request, *args, **kwargs):
    pk = kwargs.get('pk')
    pdf = get_object_or_404(Story, pk=pk)
 
    template_path = 'pages/pdf.html'
    context = {'pdf': pdf}
    # Create a Django response object, and specify content_type as pdf
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="report.pdf"'
    # find the template and render it.
    template = get_template(template_path)
    html = template.render(context)
 
    # create a pdf
    pisa_status = pisa.CreatePDF(
       html.encode("UTF-8"), dest=response, encoding='UTF-8', link_callback=link_callback)
    # if error then show some funny view
    if pisa_status.err:
       return HttpResponse('We had some errors <pre>' + html + '</pre>')
    return response

Шаблон:

 <!DOCTYPE html>
<html lang="ru">
 
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Title</title>
<style>
    @font-face {
        font-family: Raleway;
        src: url('static/pages/fonts/Montserrat-Regular.woff2');
    }
    body { 
        font-family: Raleway, serif;
    }
</style>
</head>
<body>
dfgdfgdfgdfgdfgdfgdgdfg
{{ pdf.content }}
</body>
</html>

Шрифт по адресу: 127.0.0.1:8000/static/pages/fonts/Montserrat-Regular.woff2 корректно скачивается


Ответы (0 шт):