我想使用 pisa 为 pdf 文件生成一个 html 模板。我相信我拥有我需要的所有包裹,但我似乎在这样做时遇到了问题。下面是我的观点 到目前为止我做了什么。
编辑:这是我最新的网址、 View 和模板。
网址.py
(r'^index/render_pdf/(?P<id>\d+)/$', render_pdf),
View .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
关于python - 比萨的 Django pdf 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4678723/