Django pdf 问题与 pisa

发布于 2024-10-11 11:41:40 字数 774 浏览 4 评论 0原文

我想使用 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 %}">

I want to generate a html template to a pdf file using pisa. I believe I have all the packages I need but I seem to be having problems doing so. Here is my view below so
far what I have done.

EDIT: Here is my latest url, views & template.

url.py

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

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

In a template, I have this tag.

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

views.py

In a template, I have this tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋风の叶未落 2024-10-18 11:41:40

我不知道这有多大帮助,但这是我用来渲染 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

I dont know how much this will help, but this is the function i use to render the 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
客…行舟 2024-10-18 11:41:40

只是为了好玩,请尝试以下操作:

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)

如果您仍然遇到错误,我猜测错误可能出在比萨中。您确定它是最新的吗?

Just for fun, try this instead:

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)

If you still encounter an error, I'm guessing the error might be in pisa. Are you sure it's up to date?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文