From 1f1374f7600d911b82dbe308b6de791bde592783 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Mon, 21 May 2012 15:26:10 +0100 Subject: Don't remove tmp files before they're used. Put _tmp_files onto the object and remove them after the output has been created. --- wkhtmltopdf/views.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'wkhtmltopdf/views.py') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index b15d7be..cf141ee 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -1,4 +1,4 @@ -from os import remove +import os from re import compile from django.conf import settings @@ -33,6 +33,11 @@ class PDFTemplateView(TemplateView): margin_right = 0 margin_top = 0 response = PDFResponse + _tmp_files = None + + def __init__(self, *args, **kwargs): + self._tmp_files = [] + super(PDFTemplateView, self).__init__(*args, **kwargs) def get(self, request, context_instance=None, *args, **kwargs): if request.GET.get('as', '') == 'html': @@ -45,7 +50,10 @@ class PDFTemplateView(TemplateView): page_path = template_to_temp_file(self.get_template_names(), self.get_context_data(), self.context_instance) pdf_kwargs = self.get_pdf_kwargs() - return self.response(wkhtmltopdf(page_path, **pdf_kwargs), self.get_filename()) + output = wkhtmltopdf(page_path, **pdf_kwargs) + if self._tmp_files: + map(os.remove, self._tmp_files) + return self.response(output, self.get_filename()) def get_filename(self): return self.filename @@ -58,14 +66,12 @@ class PDFTemplateView(TemplateView): 'margin_top': self.margin_top, 'orientation': self.orientation, } - tmp_files = [] if self.header_template: kwargs['header_html'] = template_to_temp_file(self.header_template, self.get_context_data(), self.context_instance) - tmp_files.append(kwargs['header_html']) + self._tmp_files.append(kwargs['header_html']) if self.footer_template: kwargs['footer_html'] = template_to_temp_file(self.footer_template, self.get_context_data(), self.context_instance) - tmp_files.append(kwargs['footer_html']) - map(remove, tmp_files) + self._tmp_files.append(kwargs['footer_html']) return kwargs def get_context_data(self, **kwargs): -- cgit v1.2.3 From a9e1c24ea0f810863c4d611148c41a24afe4ec62 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Mon, 21 May 2012 15:52:38 +0100 Subject: Only set 'Content-Disposition' header if filename is set Setting 'Content-Disposition' explicitly sets the PDF as an attachment causing browsers to download the PDF. However newer browsers, like Chrome, will display the PDF without this header. So assume the dev wants this to be force download if they set the filename. --- wkhtmltopdf/views.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'wkhtmltopdf/views.py') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index cf141ee..36efaf5 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -11,9 +11,11 @@ from wkhtmltopdf.utils import template_to_temp_file, wkhtmltopdf class PDFResponse(HttpResponse): - def __init__(self, content, filename): + def __init__(self, content, **kwargs): super(PDFResponse, self).__init__(content, 'application/pdf') - self.__setitem__('Content-Disposition', 'attachment; filename=%s' % filename) + if 'filename' in kwargs: + header_content = 'attachment; filename={0}'.format(kwargs.get('filename')) + self.__setitem__('Content-Disposition', header_content) class PdfResponse(PDFResponse): -- cgit v1.2.3 From 2fc62e5be9f0f4394507505c92b5174532db5e10 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Mon, 21 May 2012 15:56:21 +0100 Subject: Pass around args and kwargs in PDFResponse In case you want to pass more things up to HttpResponse. Can't pass filename down though. --- wkhtmltopdf/views.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'wkhtmltopdf/views.py') diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 36efaf5..164f94f 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -11,10 +11,11 @@ from wkhtmltopdf.utils import template_to_temp_file, wkhtmltopdf class PDFResponse(HttpResponse): - def __init__(self, content, **kwargs): - super(PDFResponse, self).__init__(content, 'application/pdf') - if 'filename' in kwargs: - header_content = 'attachment; filename={0}'.format(kwargs.get('filename')) + def __init__(self, content, *args, **kwargs): + filename = kwargs.pop('filename', None) + super(PDFResponse, self).__init__(content, 'application/pdf', *args, **kwargs) + if filename: + header_content = 'attachment; filename={0}'.format(filename) self.__setitem__('Content-Disposition', header_content) @@ -55,7 +56,7 @@ class PDFTemplateView(TemplateView): output = wkhtmltopdf(page_path, **pdf_kwargs) if self._tmp_files: map(os.remove, self._tmp_files) - return self.response(output, self.get_filename()) + return self.response(output, filename=self.get_filename()) def get_filename(self): return self.filename -- cgit v1.2.3