无法加载图像/文件pisa pdf Django python [英] Unable to load images/files pisa pdf Django python
问题描述
即使我指定了 @ font-face
来使用UTF-8字体,我也不会显示汉字。原来我不能显示图像...所以我似乎我无法获取任何嵌入到我的pdf文件。
I had a problem before where it wouldn't show Chinese characters even when I specified @font-face
to use a UTF-8 font. It turns out I cannot display images as well... so I seems like I am unable to get any of the files embeded into my 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 path
html
<img src="/static/images/bc_logo_bw_pdf.png" />
和
@font-face {
font-family: "Wingdings";
src: url("/static/fonts/wingdings.ttf");
}
我看过SO上的其他任务,但没有帮助。这两个功能也没有例外。另外在 fetch_resources
函数中,返回的路径是文件的正确完整路径,即 / home /< user> / project / static / images / bc_logo_bw_pdf .png
和 /home/<user>/project/static/fonts/wingdings.ttf
,我失去了什么
I looked at the other quests on SO but it was no help. There are also no exceptions happening in the two functions. Also in fetch_resources
function the path returned was the correct full path to the file i.e. /home/<user>/project/static/images/bc_logo_bw_pdf.png
and /home/<user>/project/static/fonts/wingdings.ttf
and I am at a loss as to what is wrong.
更新
每次我创建一个pdf,我在控制台上收到这个消息
UPDATE Everytime I create a pdf, I get this message on the console
No handlers could be found for logger "ho.pisa"
可以相关吗?
更新#2
字体现在我做了一个愚蠢的错误...我使用的字体没有中文unicode。但我仍然无法将任何图像嵌入到pdf中,无论是jpeg,gif还是png。
The font works now I made a dumb mistake... The font I was using did not have the Chinese unicode. But I still cannot embed any images onto the pdf, be it jpeg, gif or png.
推荐答案
我终于解决了问题我正在...事实证明,如果我设置了正文
的高度与css ...一旦我删除了图像正在加载的行,它不起作用...
I have finally solved the problem I was having... it turns out it doesn't work if I set the body
's height with css... once I removed that line the image was loading perfectly...
这篇关于无法加载图像/文件pisa pdf Django python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!