Django - 创建和创建使用 XHTML2PDF 存储 PDF 文件

发布于 12-20 12:22 字数 114 浏览 4 评论 0原文

到目前为止,我们正在使用 XHTML2PDF 动态生成 PDF 并在需要时输出到浏览器。现在我们的要求改为只生成一次PDF并将其存储在服务器中。应向用户显示链接以查看 PDF。您能否指出实现此目标的任何资源或片段?

As of now we are using XHTML2PDF to dynamically generate PDFs and outputting to browser whenever required. Now our requirements is changed to generate the PDF only once and store it in the server. The link should be displayed to user to view the PDF. Could you please point out any resources or snippets to achieve this?

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

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

发布评论

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

评论(1

请持续率性2024-12-27 12:22:21

这很容易做到。观察:

from django.core.files.base import ContentFile

# get_pdf_contents should return the binary information for
# a properly formed pdf doc.
pdf_contents = get_pdf_contents()

file_to_be_saved = ContentFile(pdf_contents)

item = Item.objects.get(pk=1)

item.myfilefield.save('blarg.pdf', file_to_be_saved)

get_pdf_contents 函数不应该太难编写 - 基本上采用您已有的任何函数并在将结果汇集到 HttpResponse 对象之前将其删除。如果您需要帮助,请发布您已有的代码。

This is pretty easy to do. Observe:

from django.core.files.base import ContentFile

# get_pdf_contents should return the binary information for
# a properly formed pdf doc.
pdf_contents = get_pdf_contents()

file_to_be_saved = ContentFile(pdf_contents)

item = Item.objects.get(pk=1)

item.myfilefield.save('blarg.pdf', file_to_be_saved)

The get_pdf_contents function shouldn't be too hard to write - basically take whatever function you have already and chop it off before it funnels the results into an HttpResponse object. If you need help with that, post the code you have already.

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