2011-01-13 44 views
5

我想使用比薩生成一個PDF文件的HTML模板。我相信我擁有所有我需要的軟件包,但我似乎遇到了問題。這裏是我的觀點,所以 遠遠是我所做的。Django pdf問題與比薩

編輯:這是我最新的網址,意見&模板。

url.py

(r'^index/render_pdf/(?P<id>\d+)/$', render_pdf), 

views.py

def fetch_resources(uri, rel): 
    path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, "")) 
    return path 

def render_pdf (html, id): 
    invoice_items_list = Invoice_Items.objects.filter(pk=id) 
    result = StringIO.StringIO() 
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), dest=result, link_callback=fetch_resources) 
    return result 

在模板中,我有這樣的標籤。

<a href="{% url c2duo.views.render_pdf invoices.pk %}"> 
+0

,你得到你所遇到的完整的錯誤回溯或行爲,並將其添加到問題,這樣每個人都可以看到以這種方式嘗試時會發生什麼。 – nosklo 2011-01-13 10:53:24

+0

`render_to_pdf(template_src,context_dict)`中沒有`return`。你能包含更多你實際使用的實際代碼嗎? – 2011-01-13 12:16:46

+0

代碼已更新 – Shehzad009 2011-01-13 12:30:49

回答

1

我不知道有多少,這將幫助,但是這是我用它來顯示PDF的功能:下一個

def fetch_resources(uri, rel): 
""" 
Callback to allow pisa/reportlab to retrieve Images,Stylesheets, etc. 
`uri` is the href attribute from the html link element. 
`rel` gives a relative path, but it's not used here. 

""" 
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, "")) 
return path 

def render_pdf (html): 
result = StringIO.StringIO() 
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), dest=result, link_callback=fetch_resources) 
return result 
0

只是爲了好玩,試試這個來代替:

def render_to_pdf(template_src, context_dict): 
    html = "<html><head><title>Title</title></head><body><h1>Hello</h1></body></html>" 
    result = StringIO.StringIO() 
    pdf = pisa.pisaDocument(StringIO.StringIO(html), result) 
    if not pdf.err: 
     return http.HttpResponse("" % (repr(result.getvalue()))) 
    else: 
     raise Exception("The error was %s" % pdf.err) 

如果仍然遇到錯誤,我猜的錯誤可能是在比薩。你確定它是最新的?