From e7c6e9357164eabd5ca6c73dc98d7ef5cdafd14a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 1 Jun 2017 20:51:07 +0100 Subject: [PATCH] Try and catch exceptions from pdf render --- md_pdf/build/pdf.py | 7 ++++++- md_pdf/exceptions.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/md_pdf/build/pdf.py b/md_pdf/build/pdf.py index b498b5d..b8d3451 100644 --- a/md_pdf/build/pdf.py +++ b/md_pdf/build/pdf.py @@ -1,6 +1,7 @@ import pdfkit from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR from md_pdf.build.templates import FILE_NAME_FORMAT +from md_pdf.exceptions import PDFRenderException import os import logging @@ -44,7 +45,7 @@ def export_pdf(content, config): PDF_OPTIONS['margin-right'] = config['context'].get('margin_horizontal', DEFAULT_MARGIN_HORIZONTAL) logger.info("Rendering PDF...") - return pdfkit.from_string( + render_ok = pdfkit.from_string( content, os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'), options=PDF_OPTIONS, @@ -52,3 +53,7 @@ def export_pdf(content, config): toc=TOC_OPTIONS if config['toc'] else {}, cover_first=True ) + if not render_ok: + raise PDFRenderException('Failed to render PDF. ' + render_ok) + return PDF_OPTIONS # mostly for testing + diff --git a/md_pdf/exceptions.py b/md_pdf/exceptions.py index a11a747..86dc882 100644 --- a/md_pdf/exceptions.py +++ b/md_pdf/exceptions.py @@ -8,3 +8,7 @@ class PrematureExit(BaseException): class ConfigValidationException(BaseException): pass + + +class PDFRenderException(BaseException): + pass