@@ -125,6 +125,23 @@ def _command(self, path=None):
125125 def command (self , path = None ):
126126 return list (self ._command (path ))
127127
128+ @staticmethod
129+ def handle_error (exit_code , stderr ):
130+ if exit_code == 0 :
131+ return
132+
133+ if 'cannot connect to X server' in stderr :
134+ raise IOError ('%s\n '
135+ 'You will need to run wkhtmltopdf within a "virtual" X server.\n '
136+ 'Go to the link below for more information\n '
137+ 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr )
138+
139+ if 'Error' in stderr :
140+ raise IOError ('wkhtmltopdf reported an error:\n ' + stderr )
141+
142+ error_msg = stderr or 'Unknown Error'
143+ raise IOError ("wkhtmltopdf exited with non-zero code {0}. error:\n {1}" .format (exit_code , error_msg ))
144+
128145 def to_pdf (self , path = None ):
129146 args = self .command (path )
130147
@@ -151,19 +168,7 @@ def to_pdf(self, path=None):
151168 stderr = stderr or stdout
152169 stderr = stderr .decode ('utf-8' , errors = 'replace' )
153170 exit_code = result .returncode
154-
155- if exit_code != 0 :
156- if 'cannot connect to X server' in stderr :
157- raise IOError ('%s\n '
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 )
161-
162- if 'Error' in stderr :
163- raise IOError ('wkhtmltopdf reported an error:\n ' + stderr )
164-
165- error_msg = stderr or 'Unknown Error'
166- raise IOError ("wkhtmltopdf exited with non-zero code {0}. error:\n {1}" .format (exit_code , error_msg ))
171+ self .handle_error (exit_code , stderr )
167172
168173 # Since wkhtmltopdf sends its output to stderr we will capture it
169174 # and properly send to stdout
0 commit comments