aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaciek Lenc <mattack108@users.noreply.github.com>2014-09-01 16:39:33 +0100
committerMaciek Lenc <mattack108@users.noreply.github.com>2014-09-01 16:39:33 +0100
commit843f95258563ac75eabb4a79fef216011f320a0a (patch)
tree0a2ce5f2383a960eed733eec1a250c5b7afbb956
parent624e04459d3282141385ff1a67f1c4673cd11f8f (diff)
parent08b50680c71b903634f7137a596ac5c0fe21562f (diff)
downloaddjango-wkhtmltopdf-843f95258563ac75eabb4a79fef216011f320a0a.tar.gz
django-wkhtmltopdf-843f95258563ac75eabb4a79fef216011f320a0a.tar.bz2
django-wkhtmltopdf-843f95258563ac75eabb4a79fef216011f320a0a.zip
Merge pull request #59 from michelts/master
Fix encoding issues in PDFTemplateResponse with python 2.x
-rw-r--r--wkhtmltopdf/tests/tests.py46
-rw-r--r--wkhtmltopdf/views.py4
2 files changed, 40 insertions, 10 deletions
diff --git a/wkhtmltopdf/tests/tests.py b/wkhtmltopdf/tests/tests.py
index f296c8e..48a8b19 100644
--- a/wkhtmltopdf/tests/tests.py
+++ b/wkhtmltopdf/tests/tests.py
@@ -17,6 +17,18 @@ from wkhtmltopdf.utils import (_options_to_args, make_absolute_paths,
from wkhtmltopdf.views import PDFResponse, PDFTemplateView, PDFTemplateResponse
+class UnicodeContentPDFTemplateView(PDFTemplateView):
+ """
+ PDFTemplateView with the addition of unicode content in his context.
+
+ Used in unicode content view testing.
+ """
+ def get_context_data(self, **kwargs):
+ Base = super(UnicodeContentPDFTemplateView, self)
+ context = Base.get_context_data(**kwargs)
+ context['title'] = u'♥'
+ return context
+
class TestUtils(TestCase):
def setUp(self):
# Clear standard error
@@ -38,7 +50,9 @@ class TestUtils(TestCase):
def test_wkhtmltopdf(self):
"""Should run wkhtmltopdf to generate a PDF"""
title = 'A test template.'
- response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title})
+ response = PDFTemplateResponse(self.factory.get('/'),
+ None,
+ context={'title': title})
temp_file = response.render_to_temporary_file('sample.html')
try:
# Standard call
@@ -59,10 +73,25 @@ class TestUtils(TestCase):
finally:
temp_file.close()
+ def test_wkhtmltopdf_with_unicode_content(self):
+ """A wkhtmltopdf call should render unicode content properly"""
+ title = u'♥'
+ response = PDFTemplateResponse(self.factory.get('/'),
+ None,
+ context={'title': title})
+ temp_file = response.render_to_temporary_file('unicode.html')
+ try:
+ pdf_output = wkhtmltopdf(pages=[temp_file.name])
+ self.assertTrue(pdf_output.startswith(b'%PDF'), pdf_output)
+ finally:
+ temp_file.close()
+
def test_PDFTemplateResponse_render_to_temporary_file(self):
"""Should render a template to a temporary file."""
title = 'A test template.'
- response = PDFTemplateResponse(self.factory.get('/'), None, context={'title': title})
+ response = PDFTemplateResponse(self.factory.get('/'),
+ None,
+ context={'title': title})
temp_file = response.render_to_temporary_file('sample.html')
temp_file.seek(0)
saved_content = smart_str(temp_file.read())
@@ -78,7 +107,7 @@ class TestViews(TestCase):
inline_fileheader = 'inline; filename="{0}"'
def test_pdf_response(self):
- """Should generate the correct HttpResponse object and content type."""
+ """Should generate correct HttpResponse object and content type."""
# 404
response = PDFResponse(content='', status=404)
self.assertEqual(response.status_code, 404)
@@ -233,11 +262,12 @@ class TestViews(TestCase):
self.test_pdf_template_view(show_content=True)
def test_pdf_template_view_unicode(self, show_content=False):
- """Test PDFTemplateView."""
-
- view = PDFTemplateView.as_view(filename=self.pdf_filename,
- show_content_in_browser=show_content,
- template_name=self.template)
+ """Test PDFTemplateView with unicode content."""
+ view = UnicodeContentPDFTemplateView.as_view(
+ filename=self.pdf_filename,
+ show_content_in_browser=show_content,
+ template_name=self.template
+ )
# As PDF
request = RequestFactory().get('/')
diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py
index 2d91224..d5f189a 100644
--- a/wkhtmltopdf/views.py
+++ b/wkhtmltopdf/views.py
@@ -5,8 +5,8 @@ from tempfile import NamedTemporaryFile
from django.conf import settings
from django.http import HttpResponse
from django.template.response import TemplateResponse
-from django.utils.encoding import smart_str
from django.views.generic import TemplateView
+from django.utils.encoding import smart_text
from .utils import (content_disposition_filename, make_absolute_paths,
wkhtmltopdf)
@@ -72,7 +72,7 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse):
context = self.resolve_context(self.context_data)
- content = smart_str(template.render(context))
+ content = smart_text(template.render(context))
content = make_absolute_paths(content)
try: