I am using pisa to generate a pdf from html in a django application. My view code is the following
if request.method == 'POST':
return write_to_pdf(request.POST['convert'], { }, 'file')
where convert is a TextArea from which i get the value to write on my pdf file
write_to_pdf
def fetch_resources(uri, rel):
path = '%s/media/pdf/' % RHOMBUS_PATH
return path
def write_to_pdf(template_data, context_dict, filename):
print template_data
template = Template(template_data)
context = Context(context_dict)
html = template.render(context)
print html
result = StringIO.StringIO()
pdf = pisa.CreatePDF(html.encode('UTF-8'), result, link_callback=fetch_resources, encoding='UTF-8')
print result.getvalue()
if not pdf.err:
response = http.HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=%s.pdf' % filename
response.write(result.getvalue())
return response
return http.HttpResponse('Problem creating PDF: %s' % cgi.escape(html))
The generated pdf though has a problem when the TextArea has greek stressed characters like
ά έ
etc. I tried changing the encodings but nothing. Any help would be appreciated.
I also had this issue with Greek characters (instead of stressed characters I received black boxes). As a first step you need to change your font to a proper one (like dejavu sans). To do this, add a
style
element to your html template like this:Now, the dejavusans font can be downloaded from http://dejavu-fonts.org/wiki/Main_Page. Also, there are various issues with the location that you will put the font files - I have provided some insight on my answer to trouble in converting unicode template to pdf using xhtml2pdf. As a first step, I propose to put these fonts in
C:/fonts
(or/tmp/fonts
if using unix) and use the absolute url for@font-face
, for instanceAfter that, check my answer to see how you can use relative urls.
Finally, I have to mention that I've only tested the above with dejavu-sans (and it works fine) - however I'd really like to know if the above solution works fine with other fonts, like Calibry - if you test it please provide feedback.
If the above doesn't work, please take a look at the
render_to_pdf
function I use: