title: Generate PDF from HTML using Django and Reportlab
body: <p>I am coming back with a new question which I am unable to answer, having scratched my head the whole day on it.</p>
  
  <p>I want to generate a PDF from a webpage by clicking on a "Download PDF" button. I tried several modules including Reportlab and XHTML2PDF but I am not able to generate any pdf nor downloading it... Here is what I did with Reportlab, following <a href="https://stackoverflow.com/questions/1377446/render-html-to-pdf-in-django-site?rq=1">Render HTML to PDF in Django site</a></p>
  
  <p>-<em>- views.py -</em>-</p>
  
  <pre><code>import cStringIO as StringIO
  import ho.pisa as pisa
  from django.template.loader import get_template
  from django.template import Context
  from django.http import HttpResponse
  from cgi import escape
  
  def index_data(request):
  #Code to generate data
       return render(request, "analytics/stat.html", locals())
       return render_to_pdf(
          'analytics/stat.html',
          {
              'pagesize':'A4',
              'mylist': results,
          }
      )
  
  def render_to_pdf(template_src, context_dict):
      template = get_template(template_src)
      context = Context(context_dict)
      html  = template.render(context)
      result = StringIO.StringIO()
  
      pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
      if not pdf.err:
          return HttpResponse(result.getvalue(), content_type='application/pdf')
      return HttpResponse('We had some errors&lt;pre&gt;%s&lt;/pre&gt;' % escape(html))
  </code></pre>
  
  <p>-<em>- urls.py -</em>-</p>
  
  <pre><code>from django.conf.urls import include, url
  from django.contrib import admin
  
  urlpatterns = [
      url(r'^admin/', include(admin.site.urls)),
      url(r'^$', "analytics.views.main_page", name="main_page"),
      url(r'^portail/$', "analytics.views.index_data", name="index"),
      url(r'^generate_pdf/$', "analytics.views.GroupPDFGenerate.as_view()", name="generate_pdf")
  </code></pre>
  
  <p>]</p>
  
  <p>-<em>- Template analytics/stat.html -</em>-</p>
  
  <pre><code>{% extends "analytics/layout.html" %}
  
  {% block title %}
      Audience
  {% endblock title %}
  
  {% block head %}
   # Script to generate google charts
  {% endblock head %}
  
  {% block body %}
  &lt;div class="page-header"&gt;
    &lt;h1 align="center"&gt; Audience &lt;/h1&gt;
  &lt;/div&gt;
  &lt;div class="row"&gt;
    &lt;div class="col-md-1"&gt;
        &lt;h3 align="center"&gt;&lt;a href="/logout/"&gt;Logout&lt;/a&gt;&lt;/h3&gt;
        &lt;h3 align="center"&gt;&lt;a href="statistiques.pdf"&gt;Download pdf&lt;/a&gt;&lt;/h3&gt;
    &lt;/div&gt;
   &lt;/div&gt;
  
  {% endblock %}
  </code></pre>
  
  <p>Also, is there a better module to process ?</p>