@@ -140,23 +140,25 @@ 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
0 commit comments