File tree Expand file tree Collapse file tree 4 files changed +58
-1
lines changed
Expand file tree Collapse file tree 4 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 11.idea
22* .pyc
3+
4+ # Build
5+ .cache
6+ .eggs
7+ pdfkit.egg-info
8+
9+ # Tests
10+ .tox
11+ .python-version
Original file line number Diff line number Diff line change 77from itertools import chain
88import io
99import codecs
10+ try :
11+ # Python 2.x and 3.x support for checking string types
12+ assert basestring
13+ except NameError :
14+ basestring = str
1015
1116
1217class PDFKit (object ):
@@ -64,6 +69,24 @@ def command(self, path=None):
6469 if self .cover and self .cover_first :
6570 args .append ('cover' )
6671 args .append (self .cover )
72+
73+ # Because certain flags require two separate arguments (e.g.
74+ # ``--custom-header``), we need to flatten those arguments so that they
75+ # are passed correctly to wkhtmltopdf
76+ #
77+ # .. seealso::
78+ #
79+ # JazzCore/python-pdfkit#45
80+ # JazzCore/python-pdfkit#53
81+ #
82+ flat_args = []
83+ for arg in args :
84+ if isinstance (arg , basestring ):
85+ flat_args .append (arg )
86+ else :
87+ flat_args .extend (arg )
88+ args = flat_args
89+
6790 if self .toc :
6891 args .append ('toc' )
6992 args += list (chain .from_iterable (list (self .toc .items ())))
@@ -160,7 +183,13 @@ def _normalize_options(self, options):
160183 normalized_key = '--%s' % self ._normalize_arg (key )
161184 else :
162185 normalized_key = self ._normalize_arg (key )
163- normalized_options [normalized_key ] = str (value ) if value else value
186+ if not isinstance (value , tuple ):
187+ normalized_options [normalized_key ] = str (value ) if value else value
188+ else :
189+ # Convert tuple values to list of strings to later be
190+ # flattened in :func:`pdfkit.PDFKit.command`
191+ normalized_options [normalized_key ] = [str (v ) if v else v for v
192+ in value ]
164193
165194 return normalized_options
166195
Original file line number Diff line number Diff line change @@ -48,6 +48,19 @@ def test_options_parsing_with_dashes(self):
4848 r = pdfkit .PDFKit ('html' , 'string' , options = {'--page-size' : 'Letter' })
4949 self .assertTrue (r .options ['--page-size' ])
5050
51+ def test_options_parsing_with_tuple (self ):
52+ r = pdfkit .PDFKit ('html' , 'string' , options = {'--custom-header' :
53+ ('Accept-Encoding' ,
54+ 'gzip' )})
55+ self .assertTrue (r .options ['--custom-header' ])
56+ r = pdfkit .PDFKit ('html' , 'string' , options = {'custom-header' :
57+ ('Accept-Encoding' ,
58+ 'gzip' )})
59+ self .assertTrue (r .options ['--custom-header' ])
60+ self .assertTrue ("--custom-header" in r .command ())
61+ self .assertTrue ("Accept-Encoding" in r .command ())
62+ self .assertTrue ("gzip" in r .command ())
63+
5164 def test_custom_configuration (self ):
5265 conf = pdfkit .configuration ()
5366 self .assertEqual ('pdfkit-' , conf .meta_tag_prefix )
Original file line number Diff line number Diff line change 1+ [tox]
2+ envlist = py27,py33,py34,py35
3+
4+ [testenv]
5+ deps = pytest
6+ commands = python setup.py test
You can’t perform that action at this time.
0 commit comments