aboutsummaryrefslogtreecommitdiffstats
path: root/wkhtmltopdf
diff options
context:
space:
mode:
Diffstat (limited to 'wkhtmltopdf')
-rw-r--r--wkhtmltopdf/tests/tests.py3
-rw-r--r--wkhtmltopdf/utils.py30
-rw-r--r--wkhtmltopdf/views.py1
3 files changed, 24 insertions, 10 deletions
diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py
index c15d88f..8f40c5e 100644
--- a/wkhtmltopdf/tests/tests.py
+++ b/wkhtmltopdf/tests/tests.py
@@ -203,7 +203,8 @@ class TestViews(TestCase):
self.assertTrue(response.has_header('Content-Disposition'))
footer_template = loader.get_template(self.footer_template)
- tempfile = render_to_temporary_file(footer_template, context=RequestContext(request, context))
+ tempfile = render_to_temporary_file(footer_template, context=context,
+ request=request)
tempfile.seek(0)
footer_content = smart_str(tempfile.read())
footer_content = make_absolute_paths(footer_content)
diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py
index 3d125bb..6be961c 100644
--- a/wkhtmltopdf/utils.py
+++ b/wkhtmltopdf/utils.py
@@ -8,7 +8,6 @@ import sys
import shlex
from tempfile import NamedTemporaryFile
-from django.template.context import Context, RequestContext
from django.utils.encoding import smart_text
try:
@@ -18,7 +17,9 @@ except ImportError: # Python2
from urllib import pathname2url
from urlparse import urljoin
+import django
from django.conf import settings
+from django.template.context import Context, RequestContext
from django.utils import six
from .subprocess import check_output
@@ -120,7 +121,7 @@ def convert_to_pdf(filename, header_filename=None, footer_filename=None, cmd_opt
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):
+def render_pdf_from_template(input_template, header_template, footer_template, context, request=None, cmd_options=None):
debug = getattr(settings, 'WKHTMLTOPDF_DEBUG', settings.DEBUG)
cmd_options = cmd_options if cmd_options else {}
@@ -131,6 +132,7 @@ def render_pdf_from_template(input_template, header_template, footer_template, c
input_file = render_to_temporary_file(
template=input_template,
context=context,
+ request=request,
prefix='wkhtmltopdf', suffix='.html',
delete=(not debug)
)
@@ -139,6 +141,7 @@ def render_pdf_from_template(input_template, header_template, footer_template, c
header_file = render_to_temporary_file(
template=header_template,
context=context,
+ request=request,
prefix='wkhtmltopdf', suffix='.html',
delete=(not debug)
)
@@ -148,6 +151,7 @@ def render_pdf_from_template(input_template, header_template, footer_template, c
footer_file = render_to_temporary_file(
template=footer_template,
context=context,
+ request=request,
prefix='wkhtmltopdf', suffix='.html',
delete=(not debug)
)
@@ -231,14 +235,22 @@ def make_absolute_paths(content):
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)
+def render_to_temporary_file(template, context, request=None, mode='w+b',
+ bufsize=-1, suffix='.html', prefix='tmp',
+ dir=None, delete=True):
+ if django.VERSION < (1, 8):
+ # If using a version of Django prior to 1.8, ensure ``context`` is an
+ # instance of ``Context``
+ if not isinstance(context, Context):
+ if request:
+ context = RequestContext(request, context)
+ else:
+ context = Context(context)
+ content = template.render(context)
+ else:
+ content = template.render(context, request)
- content = smart_text(template.render(context))
+ content = smart_text(content)
content = make_absolute_paths(content)
try:
diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py
index f26c9ae..dff87b4 100644
--- a/wkhtmltopdf/views.py
+++ b/wkhtmltopdf/views.py
@@ -75,6 +75,7 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse):
self.resolve_template(self.header_template),
self.resolve_template(self.footer_template),
context=self.resolve_context(self.context_data),
+ request=self._request,
cmd_options=cmd_options
)