From bac0816fa90b8eab949453a5abf34db2e6d4883c Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 4 May 2017 00:06:00 -0700 Subject: [PATCH 1/3] only treat stderr as text after getting non-zero exit code, otherwise it should be pdf binary data --- pdfkit/pdfkit.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/pdfkit/pdfkit.py b/pdfkit/pdfkit.py index dc56174..65c457f 100644 --- a/pdfkit/pdfkit.py +++ b/pdfkit/pdfkit.py @@ -140,23 +140,25 @@ def to_pdf(self, path=None): input = None stdout, stderr = result.communicate(input=input) stderr = stderr or stdout - try: - stderr = stderr.decode('utf-8') - except UnicodeDecodeError: - stderr = '' + exit_code = result.returncode + if exit_code != 0: + try: + stderr = stderr.decode('utf-8') + except UnicodeDecodeError: + stderr = '' - if 'cannot connect to X server' in stderr: - raise IOError('%s\n' - 'You will need to run wkhtmltopdf within a "virtual" X server.\n' - 'Go to the link below for more information\n' - 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr) + if 'cannot connect to X server' in stderr: + raise IOError('%s\n' + 'You will need to run wkhtmltopdf within a "virtual" X server.\n' + 'Go to the link below for more information\n' + 'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr) - if 'Error' in stderr: - raise IOError('wkhtmltopdf reported an error:\n' + stderr) + if 'Error' in stderr: + raise IOError('wkhtmltopdf reported an error:\n' + stderr) - if exit_code != 0: - raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, stderr)) + error_msg = stderr or 'Unknown Error' + raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, error_msg)) # Since wkhtmltopdf sends its output to stderr we will capture it # and properly send to stdout From d667a337428c61144bd85cfdf92926d717c3b6c8 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 4 May 2017 00:08:27 -0700 Subject: [PATCH 2/3] decode stderr before writing to stdout --- pdfkit/pdfkit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfkit/pdfkit.py b/pdfkit/pdfkit.py index 65c457f..1d0aada 100644 --- a/pdfkit/pdfkit.py +++ b/pdfkit/pdfkit.py @@ -163,7 +163,7 @@ def to_pdf(self, path=None): # Since wkhtmltopdf sends its output to stderr we will capture it # and properly send to stdout if '--quiet' not in args: - sys.stdout.write(stderr) + sys.stdout.write(stderr.decode('utf-8')) if not path: return stdout From 438b9b7abcb2045df2b5dc71e6e0a9f5601c7253 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Thu, 4 May 2017 00:19:08 -0700 Subject: [PATCH 3/3] catch OSError in case output file does not exist --- pdfkit/pdfkit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfkit/pdfkit.py b/pdfkit/pdfkit.py index 1d0aada..9aec4f4 100644 --- a/pdfkit/pdfkit.py +++ b/pdfkit/pdfkit.py @@ -177,7 +177,7 @@ def to_pdf(self, path=None): 'Check whhtmltopdf output without \'quiet\' ' 'option' % ' '.join(args)) return True - except IOError as e: + except (IOError, OSError) as e: raise IOError('Command failed: %s\n' 'Check whhtmltopdf output without \'quiet\' option\n' '%s ' %(' '.join(args)),e)