@@ -127,9 +127,14 @@ def command(self, path=None):
127
127
128
128
def to_pdf (self , path = None ):
129
129
args = self .command (path )
130
-
131
- result = subprocess .Popen (args , stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
132
- stderr = subprocess .PIPE , env = self .environ )
130
+
131
+ result = subprocess .Popen (
132
+ args ,
133
+ stdin = subprocess .PIPE ,
134
+ stdout = subprocess .PIPE ,
135
+ stderr = subprocess .PIPE ,
136
+ env = self .environ
137
+ )
133
138
134
139
# If the source is a string then we will pipe it into wkhtmltopdf.
135
140
# If we want to add custom CSS to file then we read input file to
@@ -141,18 +146,18 @@ def to_pdf(self, path=None):
141
146
input = self .source .source .read ().encode ('utf-8' )
142
147
else :
143
148
input = None
149
+
144
150
stdout , stderr = result .communicate (input = input )
145
151
stderr = stderr or stdout
146
-
152
+ stderr = stderr . decode ( 'utf-8' , errors = 'replace' )
147
153
exit_code = result .returncode
148
- if exit_code != 0 :
149
- stderr = stderr .decode ('utf-8' , errors = 'replace' )
150
154
155
+ if exit_code != 0 :
151
156
if 'cannot connect to X server' in stderr :
152
157
raise IOError ('%s\n '
153
- 'You will need to run wkhtmltopdf within a "virtual" X server.\n '
154
- 'Go to the link below for more information\n '
155
- 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr )
158
+ 'You will need to run wkhtmltopdf within a "virtual" X server.\n '
159
+ 'Go to the link below for more information\n '
160
+ 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr )
156
161
157
162
if 'Error' in stderr :
158
163
raise IOError ('wkhtmltopdf reported an error:\n ' + stderr )
@@ -163,24 +168,24 @@ def to_pdf(self, path=None):
163
168
# Since wkhtmltopdf sends its output to stderr we will capture it
164
169
# and properly send to stdout
165
170
if '--quiet' not in args :
166
- sys .stdout .write (stderr . decode ( 'utf-8' , errors = 'replace' ) )
171
+ sys .stdout .write (stderr )
167
172
168
173
if not path :
169
174
return stdout
170
- else :
171
- try :
172
- with codecs .open (path , encoding = 'utf-8' ) as f :
173
- # read 4 bytes to get PDF signature '%PDF'
174
- text = f .read (4 )
175
- if text == '' :
176
- raise IOError ('Command failed: %s\n '
177
- 'Check whhtmltopdf output without \' quiet\' '
178
- 'option' % ' ' .join (args ))
179
- return True
180
- except (IOError , OSError ) as e :
181
- raise IOError ('Command failed: %s\n '
182
- 'Check whhtmltopdf output without \' quiet\' option\n '
183
- '%s ' % (' ' .join (args ), e ))
175
+
176
+ try :
177
+ with codecs .open (path , encoding = 'utf-8' ) as f :
178
+ # read 4 bytes to get PDF signature '%PDF'
179
+ text = f .read (4 )
180
+ if text == '' :
181
+ raise IOError ('Command failed: %s\n '
182
+ 'Check whhtmltopdf output without \' quiet\' '
183
+ 'option' % ' ' .join (args ))
184
+ return True
185
+ except (IOError , OSError ) as e :
186
+ raise IOError ('Command failed: %s\n '
187
+ 'Check whhtmltopdf output without \' quiet\' option\n '
188
+ '%s ' % (' ' .join (args ), e ))
184
189
185
190
def _normalize_options (self , options ):
186
191
""" Generator of 2-tuples (option-key, option-value).
@@ -206,7 +211,6 @@ def _normalize_options(self, options):
206
211
else :
207
212
yield (normalized_key , str (value ) if value else value )
208
213
209
-
210
214
def _normalize_arg (self , arg ):
211
215
return arg .lower ()
212
216
0 commit comments