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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top