Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Creating & Store PDF Files using XHTML2PDF

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?

like image 975
Sivasubramaniam Arunachalam Avatar asked Dec 11 '11 06:12

Sivasubramaniam Arunachalam


People also ask

What does Django create?

Django provides a powerful form library that handles rendering forms as HTML, validating user-submitted data, and converting that data to native Python types. Django also provides a way to generate forms from your existing models and use those forms to create and update data.

Is Django easy for beginners?

It's not easy to learn Django if you don't have a strong foundational knowledge of Python. You don't need to learn everything in Python but at least make your fundamental concepts clear in Python to start with the Django application. Focus especially on classes and object-oriented programming in Python.


1 Answers

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.

like image 132
Evan Brumley Avatar answered Sep 30 '22 18:09

Evan Brumley