[英]Pisa (XHTML -> PDF) in Django will not display images in PDF
[英]Unable to load images/files pisa pdf Django python
我遇到了一個問題,即使我指定@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 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
和我不知所措。
更新每次創建pdf時,都會在控制台上收到此消息
No handlers could be found for logger "ho.pisa"
這可能相關嗎?
更新#2
字體有效,現在我犯了一個愚蠢的錯誤...我使用的字體沒有中文unicode。 但是我仍然無法在pdf上嵌入任何圖像,無論是jpeg,gif還是png。
我終於解決了我遇到的問題...事實證明,如果我使用css設置body
的高度,那將是行不通的...一旦我刪除了那條線,圖像就可以完美加載了...
對我來說(django 1.4,python 2.7 pisa == 3.0.33),如果我放置圖像的完整路徑而不是相對路徑,它對我有用。 嘗試做同樣的事情。
一切看起來都更好。 嘗試使用JPG圖像文件。 就我而言,PNG文件也不起作用。
<img src="/static/images/<name>.jpg" />
沒有width
和height
屬性的圖片將無法工作。 添加width和height屬性。
<img src="{% static 'images/logo.png' %}" alt="image" width="200" height="150" />
此修復程序對我有用。
我在這里有同樣的問題。 不要放棄XHTML2PDF Pisa。
比薩使用PIL生成PDF,並使用lib zip解碼器插入圖像。
您應該檢查您的PIL是否已經正確安裝了zip解碼器,字體和一些組件
我已經通過安裝帶有zip解碼器的PIL解決了這個問題。
http://obroll.com/install-python-pil-python-image-library-on-ubuntu-11-10-oneiric/
如果您需要更多詳細信息,可以在這里閱讀我的文章: http : //obroll.com/how-to-load-images-files-style-css-in-pdf-using-pisa-xhtml2pdf-on-django/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.