asadlive84

views.py for pdf

Nov 8th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. html_string = render_to_string('shop/pdf_invoice.html', context)
  2.  
  3.     with codecs.open('out.html', 'w', 'utf-8') as f:
  4.        f.write(html_string)
  5.        
  6.     html = HTML(string=html_string)
  7.     result = html.write_pdf()
  8.  
  9.     response = HttpResponse(content_type='application/pdf;')
  10.     response['Content-Disposition'] = 'inline; filename=invoice' + \
  11.         str(customers.id)+customers.customer_uid + customers.customer_name + '.pdf'
  12.     response['Content-Transfer-Encoding'] = 'utf-8'
  13.  
  14.     with tempfile.NamedTemporaryFile(mode='wb+') as output:
  15.         output.write(result)
  16.         output.flush()
  17.         output = open(output.name, 'wb+')
  18.         response.write(output.read())
  19.  
  20.     return response
Advertisement
Add Comment
Please, Sign In to add comment