@@ -140,28 +140,30 @@ def to_pdf(self, path=None):
140140 input = None
141141 stdout , stderr = result .communicate (input = input )
142142 stderr = stderr or stdout
143- try :
144- stderr = stderr .decode ('utf-8' )
145- except UnicodeDecodeError :
146- stderr = ''
143+
147144 exit_code = result .returncode
145+ if exit_code != 0 :
146+ try :
147+ stderr = stderr .decode ('utf-8' )
148+ except UnicodeDecodeError :
149+ stderr = ''
148150
149- if 'cannot connect to X server' in stderr :
150- raise IOError ('%s\n '
151- 'You will need to run wkhtmltopdf within a "virtual" X server.\n '
152- 'Go to the link below for more information\n '
153- 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr )
151+ if 'cannot connect to X server' in stderr :
152+ 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 )
154156
155- if 'Error' in stderr :
156- raise IOError ('wkhtmltopdf reported an error:\n ' + stderr )
157+ if 'Error' in stderr :
158+ raise IOError ('wkhtmltopdf reported an error:\n ' + stderr )
157159
158- if exit_code != 0 :
159- raise IOError ("wkhtmltopdf exited with non-zero code {0}. error:\n {1}" .format (exit_code , stderr ))
160+ error_msg = stderr or 'Unknown Error'
161+ raise IOError ("wkhtmltopdf exited with non-zero code {0}. error:\n {1}" .format (exit_code , error_msg ))
160162
161163 # Since wkhtmltopdf sends its output to stderr we will capture it
162164 # and properly send to stdout
163165 if '--quiet' not in args :
164- sys .stdout .write (stderr )
166+ sys .stdout .write (stderr . decode ( 'utf-8' ) )
165167
166168 if not path :
167169 return stdout
@@ -175,7 +177,7 @@ def to_pdf(self, path=None):
175177 'Check whhtmltopdf output without \' quiet\' '
176178 'option' % ' ' .join (args ))
177179 return True
178- except IOError as e :
180+ except ( IOError , OSError ) as e :
179181 raise IOError ('Command failed: %s\n '
180182 'Check whhtmltopdf output without \' quiet\' option\n '
181183 '%s ' % (' ' .join (args )),e )
0 commit comments