From 48956872062023691da1fd9045c6a4c3277d0a71 Mon Sep 17 00:00:00 2001 From: Andy Lee Date: Wed, 30 Nov 2016 22:57:02 -0600 Subject: [PATCH] Adding ability to ignore error codes wkhtml can return error codes whilst the pdf is still properly generated. This can happen if wkhtmltopdf receives an http error while retrieving a link. https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1877 This allows you to pass an to ignore any error codes that wkhtmltopdf returns. The default behavior is still to not ignore them. --- app.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 273f15b6..9cb615e0 100644 --- a/app.py +++ b/app.py @@ -33,11 +33,14 @@ def application(request): payload = json.loads(request.data) source_file.write(payload['contents'].decode('base64')) options = payload.get('options', {}) + ignoreErrors = payload.get('ignoreerrors',False) elif request.files: # First check if any files were uploaded source_file.write(request.files['file'].read()) # Load any options that may have been provided in options options = json.loads(request.form.get('options', '{}')) + # Optionally bypass any excpetions thrown from wkhtmltopdf + ignoreErrors = json.loads(request.form.get('ignoreerrors', 'false')) source_file.flush() @@ -56,7 +59,11 @@ def application(request): args += [file_name, file_name + ".pdf"] # Execute the command using executor - execute(' '.join(args)) + try: + execute(' '.join(args)) + except: + if not ignoreErrors: + raise return Response( wrap_file(request.environ, open(file_name + '.pdf')),