title: Preview PDF document
body: <p>If I run: </p>
  
  <blockquote>
    <p>127.0.0.1:8000/document/1/preview</p>
  </blockquote>
  
  <p>this pdf file is downloaded.</p>
  
  <p>I need display it in HTML(preview with print function). How to do it?</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>from xhtml2pdf import pisa
  from django.template.loader import render_to_string
  from datetime import datetime
  import StringIO
  
  def pdf_report(request, did):
      d_instance = get_object_or_404(MyObject, pk=did, user=request.user)
  
      contents = render_to_string('pdf_preview.html', {'object':d_instance})
      response = HttpResponse(mimetype='application/pdf')
      response['Content-Disposition'] = 'attachment; filename=answer_%s.pdf' % (f_date,)
      result = StringIO.StringIO()
      pdf = pisa.pisaDocument(StringIO.StringIO(contents.encode('utf-8')), result, show_error_as_pdf=True, encoding='UTF-8')
      if not pdf.err:
          response.write(result.getvalue())
          result.close()
          return response
  </code></pre>
  
  <p><strong>urls.py</strong></p>
  
  <pre><code>(r'^document/(?P&lt;did&gt;\d+)/preview/$', 'app.views.pdf_report'),
  </code></pre>