4
4
import sys
5
5
from .source import Source
6
6
from .configuration import Configuration
7
- from itertools import chain
8
7
import io
9
8
import codecs
10
9
try :
@@ -141,24 +140,28 @@ def to_pdf(self, path=None):
141
140
input = None
142
141
stdout , stderr = result .communicate (input = input )
143
142
stderr = stderr or stdout
143
+ try :
144
+ stderr = stderr .decode ('utf-8' )
145
+ except UnicodeDecodeError :
146
+ stderr = ''
144
147
exit_code = result .returncode
145
148
146
- if 'cannot connect to X server' in stderr . decode ( 'utf-8' ) :
149
+ if 'cannot connect to X server' in stderr :
147
150
raise IOError ('%s\n '
148
151
'You will need to run wkhtmltopdf within a "virtual" X server.\n '
149
152
'Go to the link below for more information\n '
150
- 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr . decode ( 'utf-8' ) )
153
+ 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr )
151
154
152
- if 'Error' in stderr . decode ( 'utf-8' ) :
153
- raise IOError ('wkhtmltopdf reported an error:\n ' + stderr . decode ( 'utf-8' ) )
155
+ if 'Error' in stderr :
156
+ raise IOError ('wkhtmltopdf reported an error:\n ' + stderr )
154
157
155
158
if exit_code != 0 :
156
- raise IOError ("wkhtmltopdf exited with non-zero code {0}. error:\n {1}" .format (exit_code , stderr . decode ( "utf-8" ) ))
159
+ raise IOError ("wkhtmltopdf exited with non-zero code {0}. error:\n {1}" .format (exit_code , stderr ))
157
160
158
161
# Since wkhtmltopdf sends its output to stderr we will capture it
159
162
# and properly send to stdout
160
163
if '--quiet' not in args :
161
- sys .stdout .write (stderr . decode ( 'utf-8' ) )
164
+ sys .stdout .write (stderr )
162
165
163
166
if not path :
164
167
return stdout
@@ -184,18 +187,17 @@ def _normalize_options(self, options):
184
187
:param options: dict {option name: value}
185
188
186
189
returns:
187
- iterator (option-key, option-value)
190
+ iterator (option-key, option-value)
188
191
- option names lower cased and prepended with
189
192
'--' if necessary. Non-empty values cast to str
190
193
"""
191
- normalized_options = {}
192
194
193
195
for key , value in list (options .items ()):
194
196
if not '--' in key :
195
197
normalized_key = '--%s' % self ._normalize_arg (key )
196
198
else :
197
199
normalized_key = self ._normalize_arg (key )
198
-
200
+
199
201
if isinstance (value , (list , tuple )):
200
202
for optval in value :
201
203
yield (normalized_key , optval )
0 commit comments