Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using wkhtmltopdf with dynamic templates

I want to generate a PDf of a django template / view; that uses templatetags etc.

From django-wkhtmltopdf's documentation:

from django.conf.urls.defaults import url, patterns
from wkhtmltopdf.views import PDFTemplateView

urlpatterns = patterns('',
    url(r'^pdf/$', PDFTemplateView.as_view(template_name='my_template.html',
                                           filename='my_pdf.pdf'), name='pdf'),
)

Or they say in your own view:

from wkhtmltopdf.views import PDFTemplateView


class MyPDF(PDFTemplateView):
    filename = 'my_pdf.pdf'
    template_name = 'my_template.html'
    cmd_options = {
        'margin-top': 3,
    }

If this was my view:

def download_report(request):
    vends = Vends.objects.all()
    return render(request, 'report_template.html', {'vends':vends}) 

How would I generate a report of this VIEW as its rendered? Not just the template, because that would be useless?

like image 446
Harry Avatar asked Dec 26 '22 01:12

Harry


1 Answers

You can use PDFTemplateRespone instead of render_to_response.

from wkhtmltopdf.views import PDFTemplateResponse 

Then you can do something like this:

def pdf(request, pk):   
     context = RequestContext(request)    
     template = 'test.html'

     context = {
        'variable1': variable1,
        'variable1': variable1,
        'variable1': variable1,
        'variable1': variable1

    }


    return PDFTemplateResponse(request=request, cmd_options={'disable-javascript':True}, template=template, context=context)
like image 177
user3883987 Avatar answered Jan 04 '23 15:01

user3883987