簡體   English   中英

Django與比薩的pdf問題

[英]Django pdf question with pisa

我想使用pisa為pdf文件生成一個html模板。 我相信我擁有我需要的所有包裹,但我似乎遇到了問題。 以下是我到目前為止的觀點。

編輯:這是我最新的網址,視圖和模板。

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 %}">

我不知道這有多大幫助,但這是我用來渲染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

只是為了好玩,試試這個:

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)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM