Answer the question
In order to leave comments, you need to log in
How to make Pisa friends with Cyrillic?
Hello everyone, I can’t figure out how to make Pisa with Cyrillic, for printing pdf, I’m trying to connect the font but it doesn’t work.
How to be?
# encoding: utf-8
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_pdf(url_template, contexto = {}):
template = get_template(url_template)
html = template.render(contexto)
result = BytesIO()
pdf = pisa.pisaDocument(html.encode('UTF-8'), result, encodind = 'UTF-8')
if not pdf.err:
return HttpResponse(result.getvalue(), content_type = "application/pdf")
return None
from __future__ import unicode_literals
from django.shortcuts import render
from django.views.generic import View
# Create your views here.
from django.http import HttpResponse
from mi_pdf.utileria import render_pdf
class PDFPrueba(View):
'''
Пишем вывод пдф
'''
def get(self, request, *args, **kwargs):
pdf = render_pdf("profiles/dogovor/dogovor.html")
return HttpResponse(pdf ,content_type = "application/pdf")
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
@font-face { font-family: Arial; src: url("profiles/dogovor/arial.ttf"); }
body{
font-family: Arial;
}
</style>
Answer the question
In order to leave comments, you need to log in
The renderer can't load links itself, so it can't get the profiles/dogovor/arial.ttf font. The pisaDocument function has a link_callback parameter, to which you can pass a function that converts http addresses to local paths. For example this one:
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
pdf = pisa.pisaDocument(BytesIO(template.encode('UTF-8')), result,
encoding='utf-8',
link_callback=fetch_pdf_resources)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question