jazzband / django-downloadview

Serve files with Django.
https://django-downloadview.readthedocs.io
Other
380 stars 58 forks source link

Use VirtualDownloadView to generate downloadable PDF file #77

Closed sfdye closed 7 years ago

sfdye commented 10 years ago

Basically I want to generate downloadable pdf file based on dynamic html content passed by ajax from client side. The html is in the request.GET, how can I access this value in the StringIODownloadView?

From the official doc:

from StringIO import StringIO

from django_downloadview import VirtualDownloadView
from django_downloadview import VirtualFile

class StringIODownloadView(VirtualDownloadView):
    def get_file(self):
        """Return wrapper on ``StringIO`` object."""
        file_obj = StringIO(u"Hello world!\n")
        return VirtualFile(file_obj, name='hello-world.txt')

I am not sure how to use the VirtualDownloadView, since there is not a demo for this. Any help is much appreciated.

benoitbryon commented 10 years ago

Basically I want to generate downloadable pdf file based on dynamic html content passed by ajax from client side.

Yes VirtualDownloadView looks like the right one since you generate the file depending on request (i.e. the file does not persist in a storage).

The html is in the request.GET, how can I access this value in the StringIODownloadView?

You can use self.request as in any class-based view. What about something like this:

class StringIODownloadView(VirtualDownloadView):
    def get_file(self):
        """Return wrapper on ``StringIO`` object."""
        content = self.request.GET.get('message', u"Hello world!\n")
        file_obj = StringIO(content)
        return VirtualFile(file_obj, name='hello-world.txt')

Note: request, args and kwargs attributes are automatically assigned to view instances as part of the as_view() mechanisms. Code lives in django.views.generic.base.

I am not sure how to use the VirtualDownloadView, since there is not a demo for this.

All examples in demo are includes from "demo" project in code repository. For VirtualDownloadView, see demo/demoproject/virtual/views.py. Does it help? Else, can you explain what is missing, so that we can fix it?

sfdye commented 10 years ago

@benoitbryon Thank you. That's really very helpful.

I followed the steps and django-downloadview worked. Right now I ran into another problem.

So I am using xhtml2pdf to generate PDF based on dynamic HTML content from my website. I try to pass the generated PDF file to VirtualDownloadView so that the browser can prompt the user to download the PDF file. My intention is to VirtualDownloadView since there is no need to store the file on disk as it's created on the fly. However the pisa.CreatePDF method from xhtml2pdf only support create file object with Media_ROOT instead of StringIO. See here

Is there a elegant way to generate PDF from HTML and able to download on the client side? Thank you!

benoitbryon commented 10 years ago

xhtml2pdf's itself may be enough to generate PDF in StringIO files. The README says:

In-memory files can be generated by using StringIO or cStringIO instead of the file open.

See https://github.com/chrisglass/xhtml2pdf/blob/master/doc/usage.rst#basic-python-module-usage. I guess you do not need to interact with Django's storages.

That said, I do not have much experience about PDF generation. If you have more questions about it, perhaps ask on python/django IRC channels...

benoitbryon commented 10 years ago

If you find a nice (simple and short) example demonstrating how to generate PDF file on the fly, it could be a cool contribution to django-downloadview docs ;) I guess an "examples / how to serve dynamic PDF file" section can be useful to other users.

sfdye commented 10 years ago

@benoitbryon Guess I will have to figure out myself. xhtml2pdf didn't provide a working demo for generating PDF for downloading. That will be really cool, I'd love to contribute. Will let you know once I got something nice . Thank you!

benoitbryon commented 10 years ago

@sfdye: any news about generating and serving PDF files?

sfdye commented 10 years ago

@benoitbryon still no luck