how to export to pdf a table that is the result of a form in django

1

Hi, I'm doing an application in Django 1.8 and one of the things I want is for the user to fill out a form with certain search data, and I process all the data by consulting my model.object.filter and the I return the result of that query in an html in table mode. I would like once the user receives his answer in the table, he has the option to export that result to pdf. If someone could tell me how to do or leave me a link I would appreciate it.

Greetings

    
asked by Grace 09.05.2017 в 22:30
source

1 answer

1

A simple way to give the pdf user is to use the wkhtmltopdf tool. As its name suggests, it passes the html to pdf, so you will not have to rewrite styles.

link

An example that you can use with your current html:

In your urls:

from foo.views import MyPDFView

url(r'^pdf/',MyPDFView.as_view(), name='productes' ),

and in your view:

from django.views.generic.base import View
from wkhtmltopdf.views import PDFTemplateResponse

class MyPDFView(View):
    template='foo.html'

    foo1 = ....
    foo2 = ....

    context= {'foo1': productes, 'foo2':foo2 }

    def get(self, request):
        response = PDFTemplateResponse(request=request,
                                       template=self.template,
                                       header_template='header.html',
                                       footer_template='footer.html',
                                       filename="foopdf.pdf",
                                       context= self.context,
                                       show_content_in_browser=False,
                                       cmd_options={'margin-top': 25,},
                                       )
        return response
    
answered by 16.05.2017 в 20:50