Django - create and store PDF files using XHTML2PDF

We are currently using XHTML2PDF to dynamically create PDF files and output to the browser if necessary. Now our requirements are changed to generate PDF only once and store it on the server. The link should be displayed to the user to view the PDF file. Could you indicate any resources or fragments to achieve this?

+4
source share
1 answer

This is pretty easy to do. Note:

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 should not be too complicated to write - basically, execute any function that you already use and chop it off before it turns the results into an HttpResponse object. If you need help, send the code that you already have.

+5
source

Source: https://habr.com/ru/post/1385720/


All Articles