diff options
author | Simon Law <simon.law@ecometrica.com> | 2012-07-25 13:17:35 -0400 |
---|---|---|
committer | Simon Law <simon.law@ecometrica.com> | 2012-07-25 13:17:35 -0400 |
commit | 22d2721030ccc81dff23093321290b6ffda6d5fd (patch) | |
tree | b8804714bf5d7e0b5204605f5660edc49811e939 /wkhtmltopdf/views.py | |
parent | 68215e393d65eb8999fd3c26566cd7a331fe4300 (diff) | |
download | django-wkhtmltopdf-22d2721030ccc81dff23093321290b6ffda6d5fd.tar.gz django-wkhtmltopdf-22d2721030ccc81dff23093321290b6ffda6d5fd.tar.bz2 django-wkhtmltopdf-22d2721030ccc81dff23093321290b6ffda6d5fd.zip |
PDFTemplateResponse.convert_to_pdf() calls wkhtmltopdf.
Method to make it easier to override spawning of the wkhtmltopdf subprocess.
Diffstat (limited to 'wkhtmltopdf/views.py')
-rw-r--r-- | wkhtmltopdf/views.py | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index 4bceb27..ad69874 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -100,6 +100,18 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): tempfile.close() raise + def convert_to_pdf(self, filename, + header_filename=None, footer_filename=None): + cmd_options = self.cmd_options.copy() + # Clobber header_html and footer_html only if filenames are + # provided. These keys may be in self.cmd_options as hardcoded + # static files. + if header_filename is not None: + cmd_options['header_html'] = header_filename + if footer_filename is not None: + cmd_options['footer_html'] = footer_filename + return wkhtmltopdf(pages=[filename], **cmd_options) + @property def rendered_content(self): """Returns the freshly rendered content for the template and context @@ -111,9 +123,8 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): """ debug = getattr(settings, 'WKHTMLTOPDF_DEBUG', False) - cmd_options = self.cmd_options.copy() - input_file = header_file = footer_file = None + header_filename = footer_filename = None try: input_file = self.render_to_temporary_file( @@ -128,7 +139,7 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): prefix='wkhtmltopdf', suffix='.html', delete=(not debug) ) - cmd_options.setdefault('header_html', header_file.name) + header_filename = header_file.name if self.footer_template: footer_file = self.render_to_temporary_file( @@ -136,9 +147,11 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): prefix='wkhtmltopdf', suffix='.html', delete=(not debug) ) - cmd_options.setdefault('footer_html', footer_file.name) + footer_filename = footer_file.name - return wkhtmltopdf(pages=[input_file.name], **cmd_options) + return self.convert_to_pdf(filename=input_file.name, + header_filename=header_filename, + footer_filename=footer_filename) finally: # Clean up temporary files for f in filter(None, (input_file, header_file, footer_file)): |