She*_*009 5 python pdf django export views
我想使用pisa为pdf文件生成一个html模板.我相信我拥有我需要的所有包裹,但我似乎遇到了问题.以下是我到目前为止的观点.
编辑:这是我最新的网址,视图和模板.
url.py
(r'^index/render_pdf/(?P<id>\d+)/$', render_pdf),
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
在模板中,我有这个标签.
<a href="{% url c2duo.views.render_pdf invoices.pk %}">
Run Code Online (Sandbox Code Playgroud)
我不知道这有多大帮助,但这是我用来渲染 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
Run Code Online (Sandbox Code Playgroud)