Skip to content

Commit 5850063

Browse files
author
Rory McCann
committed
If you provide a bad wkhtmltopdf option, you should get an exception
If you provide an invalid wkhtmltopdf option and wkhtmltopdf fails to execute, you should be told about this straight away, by an exception being raised.
1 parent 78acf23 commit 5850063

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pdfkit/pdfkit.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ def to_pdf(self, path=None):
105105
input = None
106106
stdout, stderr = result.communicate(input=input)
107107

108+
exit_code = result.returncode
109+
108110
if 'Error' in stderr.decode('utf-8'):
109111
raise IOError('wkhtmltopdf reported an error:\n' + stderr.decode('utf-8'))
110112

113+
if exit_code != 0:
114+
raise IOError("wkhtmltopdf exited with non-zero code {0}. error:\n{1}".format(exit_code, stderr.decode("utf-8")))
115+
111116
# Since wkhtmltopdf sends its output to stderr we will capture it
112117
# and properly send to stdout
113118
if '--quiet' not in args:

tests/pdfkit-tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,5 +293,14 @@ def test_raise_error_with_wrong_css_path(self):
293293
with self.assertRaises(IOError):
294294
r.to_pdf()
295295

296+
def test_raise_error_if_bad_wkhtmltopdf_option(self):
297+
r = pdfkit.PDFKit('<html><body>Hai!</body></html>', 'string',
298+
options={'bad-option': None})
299+
with self.assertRaises(IOError) as cm:
300+
r.to_pdf()
301+
302+
raised_exception = cm.exception
303+
self.assertRegexpMatches(raised_exception.message, '^wkhtmltopdf exited with non-zero code 1. error:\nUnknown long argument --bad-option\n')
304+
296305
if __name__ == "__main__":
297306
unittest.main()

0 commit comments

Comments
 (0)