From 2c6a5f64f060a4ebcb6166068b04202f9f007ec8 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Sun, 30 Oct 2011 19:58:10 +0000 Subject: Refactor the main render method into a class based view Use a PdfResponse to deal with the headers and response type. Make the margin_* and filename variables instance variables on the view with sane defaults. --- wkhtmltopdf/utils.py | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) (limited to 'wkhtmltopdf/utils.py') diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py index b098383..4666f4c 100644 --- a/wkhtmltopdf/utils.py +++ b/wkhtmltopdf/utils.py @@ -1,9 +1,8 @@ -from os import fdopen, remove +from os import fdopen from subprocess import Popen, PIPE, CalledProcessError from tempfile import mkstemp from django.conf import settings -from django.http import HttpResponse from django.template import loader from django.utils.encoding import smart_str @@ -58,33 +57,6 @@ def wkhtmltopdf(pages, output=None, **kwargs): return output -def render_to_pdf(template_name, dictionary=None, context_instance=None, - header_template=None, footer_template=None, **kwargs): - """Renders a html template as a PDF response.""" - - filename = kwargs.pop('filename', None) - - page_path = template_to_temp_file(template_name, dictionary, context_instance) - - tmp_files = [] - if header_template is not None: - kwargs['header_html'] = template_to_temp_file(header_template, dictionary, context_instance) - tmp_files.append(kwargs['header_html']) - if footer_template is not None: - kwargs['footer_html'] = template_to_temp_file(footer_template, dictionary, context_instance) - tmp_files.append(kwargs['footer_html']) - - content = wkhtmltopdf([page_path], **kwargs) - - for path in tmp_files: - remove(path) - - response = HttpResponse(content, mimetype='application/pdf') - if filename is not None: - response['Content-Disposition'] = 'attachment;' + ' filename=%s' %filename - return response - - def template_to_temp_file(*args, **kwargs): """ Renders a template to a temp file, and returns the path of the file. @@ -94,4 +66,3 @@ def template_to_temp_file(*args, **kwargs): f.write(smart_str(loader.render_to_string(*args, **kwargs))) return tempfile_path - -- cgit v1.2.3