python - 比萨的 Django pdf 问题

标签 python django pdf views export

我想使用 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/

相关文章:

python - 确定 xlsx 单元格是否为 Excel 2007 电子表格的日期格式

python - python中大量的阶乘

python - Braintree paypal 实现到 django-python

r - 我无法使用R创建PDF输出。只能是空白页面。 (Linux)

python - 类型错误 : Object of type 'type' is not JSON serializable

python - 登录 Scrapy

python - 为什么从 django rest 框架返回的 JSON 在响应中有正斜杠?

python - Django 1.7 应用程序标签不是唯一的,重复 foo

excel - 仅允许在 Excel 2007/2010 中保存为 PDF

android - 如何从 URI 中获取文件路径?