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

feat: add in a timeout option #253

Open
wants to merge 1 commit 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
15 changes: 9 additions & 6 deletions pdfkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def from_url(url, output_path=None, options=None, toc=None, cover=None,
configuration=None, cover_first=False, verbose=False):
configuration=None, cover_first=False, verbose=False, timeout=None):
"""
Convert file of files from URLs to PDF document
Expand All @@ -17,18 +17,19 @@ def from_url(url, output_path=None, options=None, toc=None, cover=None,
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param cover_first: (optional) if True, cover always precedes TOC
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
Returns: True on success
"""

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

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, timeout=None):
"""
Convert HTML file or files to PDF document
Expand All @@ -41,18 +42,19 @@ def from_file(input, output_path=None, options=None, toc=None, cover=None, css=N
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param cover_first: (optional) if True, cover always precedes TOC
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
Returns: True on success
"""

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

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, timeout=None):
"""
Convert given string or strings to PDF document
Expand All @@ -65,12 +67,13 @@ def from_string(input, output_path=None, options=None, toc=None, cover=None, css
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param cover_first: (optional) if True, cover always precedes TOC
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
Returns: True on success
"""

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

return r.to_pdf(output_path)

Expand Down
5 changes: 3 additions & 2 deletions pdfkit/pdfkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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):
css=None, configuration=None, cover_first=False, verbose=False, timeout=None):

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

def _genargs(self, opts):
"""
Expand Down Expand Up @@ -187,7 +188,7 @@ def to_pdf(self, path=None):
else:
input = None

stdout, stderr = result.communicate(input=input)
stdout, stderr = result.communicate(input=input, timeout=self.timeout)
stderr = stderr or stdout or b""
stderr = stderr.decode('utf-8', errors='replace')
exit_code = result.returncode
Expand Down