diff options
author | Max Peterson <max@incuna.com> | 2015-12-01 14:39:58 +0000 |
---|---|---|
committer | Max Peterson <max@incuna.com> | 2015-12-01 14:39:58 +0000 |
commit | 09dcea6fb45dc18d17b254c047694c452fee12ed (patch) | |
tree | 2193e6ab9ebdb5e1598473e1417c947e5e7b7877 /wkhtmltopdf/utils.py | |
parent | 2c74c40ebc055a60d30a1424c685b6fbc96f364a (diff) | |
parent | fbf54be1da1cc90490336a57df6678a2f833a5f7 (diff) | |
download | django-wkhtmltopdf-09dcea6fb45dc18d17b254c047694c452fee12ed.tar.gz django-wkhtmltopdf-09dcea6fb45dc18d17b254c047694c452fee12ed.tar.bz2 django-wkhtmltopdf-09dcea6fb45dc18d17b254c047694c452fee12ed.zip |
Merge pull request #95 from johnraz/extract-logic
Extract logic from views
Diffstat (limited to 'wkhtmltopdf/utils.py')
-rw-r--r-- | wkhtmltopdf/utils.py | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py index 053a2e7..3d125bb 100644 --- a/wkhtmltopdf/utils.py +++ b/wkhtmltopdf/utils.py @@ -6,6 +6,10 @@ import os import re import sys import shlex +from tempfile import NamedTemporaryFile + +from django.template.context import Context, RequestContext +from django.utils.encoding import smart_text try: from urllib.request import pathname2url @@ -104,6 +108,59 @@ def wkhtmltopdf(pages, output=None, **kwargs): return check_output(ck_args, **ck_kwargs) +def convert_to_pdf(filename, header_filename=None, footer_filename=None, cmd_options=None): + # Clobber header_html and footer_html only if filenames are + # provided. These keys may be in self.cmd_options as hardcoded + # static files. + cmd_options = cmd_options if cmd_options else {} + + 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) + +def render_pdf_from_template(input_template, header_template, footer_template, context, cmd_options=None): + debug = getattr(settings, 'WKHTMLTOPDF_DEBUG', settings.DEBUG) + cmd_options = cmd_options if cmd_options else {} + + input_file = header_file = footer_file = None + header_filename = footer_filename = None + + try: + input_file = render_to_temporary_file( + template=input_template, + context=context, + prefix='wkhtmltopdf', suffix='.html', + delete=(not debug) + ) + + if header_template: + header_file = render_to_temporary_file( + template=header_template, + context=context, + prefix='wkhtmltopdf', suffix='.html', + delete=(not debug) + ) + header_filename = header_file.name + + if footer_template: + footer_file = render_to_temporary_file( + template=footer_template, + context=context, + prefix='wkhtmltopdf', suffix='.html', + delete=(not debug) + ) + footer_filename = footer_file.name + + return convert_to_pdf(filename=input_file.name, + header_filename=header_filename, + footer_filename=footer_filename, + cmd_options=cmd_options) + finally: + # Clean up temporary files + for f in filter(None, (input_file, header_file, footer_file)): + f.close() def content_disposition_filename(filename): """ @@ -145,7 +202,6 @@ def pathname2fileurl(pathname): def make_absolute_paths(content): """Convert all MEDIA files into a file://URL paths in order to correctly get it displayed in PDFs.""" - overrides = [ { 'root': settings.MEDIA_ROOT, @@ -174,3 +230,32 @@ def make_absolute_paths(content): occur[len(x['url']):]) return content + +def render_to_temporary_file(template, context, mode='w+b', bufsize=-1, + suffix='.html', prefix='tmp', dir=None, + delete=True): + # make sure the context is a context object + if not isinstance(context, (Context, RequestContext)): + context = Context(context) + + content = smart_text(template.render(context)) + content = make_absolute_paths(content) + + try: + # Python3 has 'buffering' arg instead of 'bufsize' + tempfile = NamedTemporaryFile(mode=mode, buffering=bufsize, + suffix=suffix, prefix=prefix, + dir=dir, delete=delete) + except TypeError: + tempfile = NamedTemporaryFile(mode=mode, bufsize=bufsize, + suffix=suffix, prefix=prefix, + dir=dir, delete=delete) + + try: + tempfile.write(content.encode('utf-8')) + tempfile.flush() + return tempfile + except: + # Clean-up tempfile if an Exception is raised. + tempfile.close() + raise |