我以前遇到了一个问题,即使当我指定@font-face使用UTF-8字体时,它也不会显示汉字。结果我也不能显示图像..。所以我似乎无法将任何文件嵌入到我的pdf中。
这是我使用的代码:
def render_to_pdf(template_src, context_dict):
"""Function to render html template into a pdf file"""
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")),
dest=result,
encoding='UTF-8',
link_callback=fetch_resources)
if not pdf.err:
response = http.HttpResponse(result.getvalue(), mimetype='application/pdf')
return response
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
def fetch_resources(uri, rel):
import os.path
from django.conf import settings
path = os.path.join(
settings.STATIC_ROOT,
uri.replace(settings.STATIC_URL, ""))
return pathhtml
<img src="/static/images/bc_logo_bw_pdf.png" />和
@font-face {
font-family: "Wingdings";
src: url("/static/fonts/wingdings.ttf");
}我看了看其他的任务,但没有帮助。在这两个函数中也没有例外情况。同样,在fetch_resources函数中,返回的路径是指向文件的正确的完整路径,即/home/<user>/project/static/images/bc_logo_bw_pdf.png和/home/<user>/project/static/fonts/wingdings.ttf,而我不知道哪里出了问题。
更新每次我创建一个pdf,我在控制台上得到这条消息
No handlers could be found for logger "ho.pisa"这可能是相关的吗?
更新#2
字体起作用了我犯了个愚蠢的错误。我使用的字体没有中文unicode。但我仍然不能将任何图像嵌入到pdf上,无论是jpeg、gif还是png。
发布于 2012-02-12 15:06:55
我终于解决了我的问题.结果,如果我用css设置body的高度,它就不起作用了.一旦我删除了那条线,图像就被完美地加载了.
发布于 2013-03-18 07:04:24
对于我来说(django 1.4,python2.7 pisa==3.0.33),如果我把完整的图像路径,而不是相对路径,它适用于我。试着做同样的事。
发布于 2014-04-02 18:11:34
一切看起来都好多了。试一次与JPG图像文件。在我的例子中,PNG文件也不起作用。
<img src="/static/images/<name>.jpg" />https://stackoverflow.com/questions/8959622
复制相似问题