Pisa XHTML2PDF utf encoding problem
In our eKe project we wanted to generate pdf documents from html files. We have chosen XHTML2PDF for doing this.
We have used Pisa because we needed a simple and easily integrable pdf generator. For using pisa you need to include some modules from your python code and create your own templates for generating the document. Here is an example python code fragment:
def write_pdf(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(
html.encode("UTF-8")), result)
if not pdf.err:
return http.HttpResponse(result.getvalue(), \
mimetype='application/pdf')
return http.HttpResponse('There was an error during pdf generation! %s' % cgi.escape(html))
So in theory it is very easy to use, we have manage to work it correctly for generating English texts in ten minutes. However we had a problem with Pisa when we wanted to create a PDF in Hungarian language. It was not generating the hungarian characters: ő ű Ő Ű (UTF-8 character encoding) instead it only showed rectangles. We have checked it with ISO-8859-2 encoding and it was working fine. This was not enough for us. We wanted to create a language independent program, where the users could choose from any language, from Turkish to Polish and Russian so we had to use UTF-8 encoding.
We have searched a lot of places on the Internet to solve this problem and according to the help of others this would work:
In the python code the pdf generating line is the following:
pdf = pisa.pisaDocument(StringIO.StringIO(
html.encode("UTF-8")), result, link_callback=fetch_resources,
encoding="utf-8", xhtml=True)
In the template file you have to insert this to the head section:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
And add this to your css file or html files "style" section.
@font-face {
font-family: "DejaVuSans";
src: url("http://www.toolpart.hu/static/media/medialibrary/2010/04/
DejaVuSans.ttf");
}
html {
font-family: "DejaVuSans";
}
But it still didn't worked. The problem was with the url of the font file. Somehow the program didn't recognized it. We have also tried it with
src: url( {{ MEDIA_URL }}fonts/DejaVuSans.ttf);
but it didn't worked this way.
Then we used absolute path (of django, not linux, so I think it should be no problem if you are using your application on different servers).
@font-face {
font-family: "DejaVu";
src: url(/static/fonts/DejaVuSans.ttf);
}
where /static/ is my {{ media_root }} directory.
This way you can use correct UTF-8 encodig in pdf documents generated with Pisa. Hope it helps.
blog comments powered by Disqus