Outputting PDF with Django as attachment and reload

ε祈祈猫儿з 提交于 2019-12-08 11:22:28

Don't reload. Instead create a view which is the target of the form. The view generates the PDF in a directory that is delivered from the real web server and then sends the user to the appropriated URL, where he can download the file (use HttpResponseRedirect).

Just set the forms target to the view that generates the pdf. Below I included some pros and cons for this approach vs the approach suggested by Martin (to write files to a directory the world can read)

Pros:

  • You can do authentication or special billing or whatever
  • Every request will generate a new PDF (unless you explicitly cache it)

Cons:

  • Every request will generate a new PDF
  • Memory usage
  • You have to stream a file from Django

--

Another solution would be to use the X-SendFile header of your webserver. Do a google search for instructions for your particular server. The general idea is that your view gives the webserver a path to a file which the webserver will then read directly from disk and return to the user. You can probably "force download" even these files, have a look at the documentation for your webserver on how to do it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!