Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The difference between PDFTemplateView and PDFTemplateResponse is that the view is a class-based view. And PDFTemplateResponse renders the pdf data and sets the right <a href="https://docs.djangoproject.com/en/1.5/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment">response headers</a>. To add header and footer:</p> <pre><code># urls.py from django.conf.urls.defaults import * from wkhtmltopdf.views import PDFTemplateView urlpatterns = patterns('', ... url(r'^pdf/$', PDFTemplateView.as_view(template_name='my_template.html', filename='my_pdf.pdf', header_template='my_header_template.html', footer_template='my_footer_template.html', ... ), name='pdf'), ) </code></pre> <p>Opening pdf/ in your browser will start a download of my_pdf.pdf based on the my_template.html, my_header_template.html and my_footer_template.html. </p> <p>The <a href="https://github.com/incuna/django-wkhtmltopdf/blob/master/docs/usage.rst#advanced-example">advanced example</a> shows how to subclass PDFTemplateView extending and changing the logic of PDFTemplateView. To understand what happens read <a href="https://docs.djangoproject.com/en/1.5/topics/class-based-views/intro/#using-class-based-views">Using class based views</a>.</p> <p>Like <code>header_template</code> and <code>footer_template</code> you can define a <code>response_class</code>. Because PDFTemplateResponse is the default, you don't have to define it.</p> <p><strong>EDIT</strong></p> <p>The following simple view provides you with a pdf instead of an html. This is not using django-wkhtmltopdf. You could use wkhtmltopdf in your html2pdf function.</p> <pre><code>def some_view(request): t = loader.get_template('myapp/template.html') c = RequestContext(request, {'foo': 'bar'}) html = t.render(c) pdf_data = html2pdf(html) # Your favorite html2pdf generator response = HttpResponse(pdf_data, content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="some_filename.pdf"' return response </code></pre> <p><strong>EDIT 2</strong></p> <p>A simple view with context:</p> <p>template.html</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8" /&gt; &lt;title&gt;Untitled&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;{{ title }}&lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>urls.py</p> <pre><code>from views import MyPDFView urlpatterns = patterns('', (r'^pdf/', MyPDFView.as_view()), ) </code></pre> <p>views.py</p> <pre><code>from django.views.generic.base import View from wkhtmltopdf.views import PDFTemplateResponse class MyPDFView(View): template='template.html' context= {'title': 'Hello World!'} def get(self, request): response = PDFTemplateResponse(request=request, template=self.template, filename="hello.pdf", context= self.context, show_content_in_browser=False, cmd_options={'margin-top': 50,}, ) return response </code></pre> <p><strong>EDIT 3</strong></p> <p>If you use a DetailView, you can add the object to context:</p> <pre><code>url(r'^books/(?P&lt;pk&gt;\d+)/$', MyPDFView.as_view(), name='book-detail'), class MyPDFView(DetailView): template='pdftestapp/template.html' context= {'title': 'Hello World!'} model = Book def get(self, request, *args, **kwargs): self.context['book'] = self.get_object() response=PDFTemplateResponse(request=request, template=self.template, filename ="hello.pdf", context=self.context, show_content_in_browser=False, cmd_options={'margin-top': 50,} ) return response </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload