[英]pisa html to pdf issue with greek stressed letters used with django
I am using pisa to generate a pdf from html in a django application. 我正在使用比萨在Django应用程序中从html生成pdf。 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 其中convert是一个TextArea,我从中获取要写入pdf文件的值
write_to_pdf 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 当TextArea具有像希腊语这样的压力字符时,生成的pdf会出现问题
ά έ
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).
第一步,您需要将字体更改为适当的字体(例如dejavu sans)。 To do this, add a
style
element to your html template like this: 为此,将
style
元素添加到html模板中,如下所示:
<style type='text/css'>
@font-face {
font-family: "DejaVuSansMono";
src: url("fonts/DejaVuSansMono.ttf");
}
@font-face {
font-family: "DejaVuSansMono";
src: url("fonts/DejaVuSansMono-Bold.ttf");
font-weight: bold;
}
@font-face {
font-family: "DejaVuSansMono";
src: url("fonts/DejaVuSansMono-Oblique.ttf");
font-style: italic, oblique;
}
@font-face {
font-family: "DejaVuSansMono";
src: url("fonts/DejaVuSansMono-BoldOblique.ttf");
font-weight: bold;
font-style: italic, oblique;
}
*, html {
font-family: "DejaVuSansMono";
}
html {
padding:10pt;
}
</style>
Now, the dejavusans font can be downloaded from http://dejavu-fonts.org/wiki/Main_Page . 现在,可以从http://dejavu-fonts.org/wiki/Main_Page下载dejavusans字体。 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 .
另外,放置字体文件的位置也有很多问题-我对使用xhtml2pdf将unicode模板转换为pdf时遇到的问题提供了一些见解。 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 instance 第一步,我建议将这些字体放入
C:/fonts
(如果使用unix,则为/tmp/fonts
),并为@font-face
使用绝对URL,例如
@font-face {
font-family: "DejaVuSansMono";
src: url("c:/fonts/DejaVuSansMono.ttf");
}
After that, check my answer to see how you can use relative urls. 之后,检查我的答案以查看如何使用相对URL。
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. 最后,我不得不提一下,我仅使用dejavu-sans测试了上述方法(并且工作正常)-但是,我真的想知道上述解决方案是否可以与其他字体(例如Calibry)一起使用,如果您对其进行了测试请提供反馈。
If the above doesn't work, please take a look at the render_to_pdf
function I use: 如果上述方法不起作用,请查看我使用的
render_to_pdf
函数:
def render_to_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, path= settings.PROJECT_PATH)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return HttpResponse('<pre>%s</pre>' % escape(html))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.