Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exceptions control #212

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ pdfkit.egg-info
# Tests
.tox
.python-version

# ENV
venv
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Changelog
---------
* `1.0.1`
* By default PDFKit handle errors from wkhtmltopdf. Now if you need to get clear output from wkhtmltopdf output, if it existed, you should pass "raise_exceptions=False" to API calls
* `1.0.0`
* By default PDFKit now passes "quiet" option to wkhtmltopdf. Now if you need to get output you should pass "verbose=True" to API calls
* Fix different issues with searching for wkhtmltopdf binary
Expand Down
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ You can also pass any options through meta tags in your HTML:

pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape

By default, PDFKit will handle errors from ``wkhtmltopdf``, but in some cases ``wkhtmltopdf`` return pdf data in ``stdout`` despite errors. To tell PDFKit do not handle errors from ``wkhtmltopdf`` you should pass ``raise_exceptions=False`` to API calls, but you should consider that in case of empty ``stdout`` error handling will be started anyway:

.. code-block:: python

pdfkit.from_url('google.com', 'out.pdf', raise_exceptions=False)

Configuration
-------------

Expand Down
2 changes: 1 addition & 1 deletion pdfkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

__author__ = 'Golovanov Stanislav'
__version__ = '1.0.0'
__version__ = '1.0.1'
__license__ = 'MIT'

from .pdfkit import PDFKit
Expand Down
20 changes: 10 additions & 10 deletions pdfkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from .pdfkit import Configuration


def from_url(url, output_path=None, options=None, toc=None, cover=None,
configuration=None, cover_first=False, verbose=False):
def from_url(url, output_path=None, options=None, toc=None, cover=None, configuration=None, cover_first=False,
verbose=False, raise_exceptions=True):
"""
Convert file of files from URLs to PDF document

Expand All @@ -21,14 +21,14 @@ def from_url(url, output_path=None, options=None, toc=None, cover=None,
Returns: True on success
"""

r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
configuration=configuration, cover_first=cover_first, verbose=verbose)
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover, configuration=configuration, cover_first=cover_first,
verbose=verbose, raise_exceptions=raise_exceptions)

return r.to_pdf(output_path)


def from_file(input, output_path=None, options=None, toc=None, cover=None, css=None,
configuration=None, cover_first=False, verbose=False):
configuration=None, cover_first=False, verbose=False, raise_exceptions=True):
"""
Convert HTML file or files to PDF document

Expand All @@ -45,14 +45,14 @@ def from_file(input, output_path=None, options=None, toc=None, cover=None, css=N
Returns: True on success
"""

r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css,
configuration=configuration, cover_first=cover_first, verbose=verbose)
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css, configuration=configuration,
cover_first=cover_first, verbose=verbose, raise_exceptions=raise_exceptions)

return r.to_pdf(output_path)


def from_string(input, output_path=None, options=None, toc=None, cover=None, css=None,
configuration=None, cover_first=False, verbose=False):
configuration=None, cover_first=False, verbose=False, raise_exceptions=True):
"""
Convert given string or strings to PDF document

Expand All @@ -69,8 +69,8 @@ def from_string(input, output_path=None, options=None, toc=None, cover=None, css
Returns: True on success
"""

r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css,
configuration=configuration, cover_first=cover_first, verbose=verbose)
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css, configuration=configuration,
cover_first=cover_first, verbose=verbose, raise_exceptions=raise_exceptions)

return r.to_pdf(output_path)

Expand Down
11 changes: 8 additions & 3 deletions pdfkit/pdfkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def __init__(self, msg):
def __str__(self):
return self.msg

def __init__(self, url_or_file, type_, options=None, toc=None, cover=None,
css=None, configuration=None, cover_first=False, verbose=False):
def __init__(self, url_or_file, type_, options=None, toc=None, cover=None, css=None, configuration=None,
cover_first=False, verbose=False, raise_exceptions=True):

self.source = Source(url_or_file, type_)
self.configuration = (Configuration() if configuration is None
Expand All @@ -64,6 +64,7 @@ def __init__(self, url_or_file, type_, options=None, toc=None, cover=None,
self.verbose = verbose
self.css = css
self.stylesheets = []
self.raise_exceptions = raise_exceptions

def _genargs(self, opts):
"""
Expand Down Expand Up @@ -198,7 +199,11 @@ def to_pdf(self, path=None):
stderr = stderr or stdout or b""
stderr = stderr.decode('utf-8', errors='replace')
exit_code = result.returncode
self.handle_error(exit_code, stderr)

# In some cases we don't want to handle errors if we want clean wkhtmltopdf output,
# but if we don't have stdout, we have to do it anyway
if not stdout or self.raise_exceptions:
self.handle_error(exit_code, stderr)

# Since wkhtmltopdf sends its output to stderr we will capture it
# and properly send to stdout
Expand Down
22 changes: 22 additions & 0 deletions tests/pdfkit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,27 @@ def test_issue_169_quiet_boolean_True(self):
output = r.to_pdf()
self.assertEqual(output[:4].decode('utf-8'), '%PDF')

def test_raise_exceptions_kwarg(self):

# exception raised with stdout and raise_exceptions=True
r = pdfkit.PDFKit('<html><body>Hai!</body></html>', 'string', options={'bad-option': None},
raise_exceptions=True)
with self.assertRaises(IOError):
r.to_pdf()

# exception raised despite raise_exceptions=False because no stdout
r = pdfkit.PDFKit('clearlywrongurl.asdf', 'url', raise_exceptions=False)
with self.assertRaises(IOError):
r.to_pdf()

# exception not raised with stdout and raise_exceptions=False
r = pdfkit.PDFKit('<html><body>Hai!</body></html>', 'string', options={'bad-option': None},
raise_exceptions=False)
try:
r.to_pdf()
except IOError:
self.fail("r.to_pdf() raised an IOError exception despite 'raise_exceptions=False' kwarg")


if __name__ == "__main__":
unittest.main()