Skip to content

Commit a22cab6

Browse files
committed
feat: add in a timeout option
Signed-off-by: Akhil Narang <[email protected]>
1 parent fb86d33 commit a22cab6

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

pdfkit/api.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

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

2425
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
25-
configuration=configuration, cover_first=cover_first, verbose=verbose)
26+
configuration=configuration, cover_first=cover_first, verbose=verbose, timeout=timeout)
2627

2728
return r.to_pdf(output_path)
2829

2930

3031
def from_file(input, output_path=None, options=None, toc=None, cover=None, css=None,
31-
configuration=None, cover_first=False, verbose=False):
32+
configuration=None, cover_first=False, verbose=False, timeout=None):
3233
"""
3334
Convert HTML file or files to PDF document
3435
@@ -41,18 +42,19 @@ def from_file(input, output_path=None, options=None, toc=None, cover=None, css=N
4142
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
4243
:param cover_first: (optional) if True, cover always precedes TOC
4344
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
45+
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
4446
4547
Returns: True on success
4648
"""
4749

4850
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css,
49-
configuration=configuration, cover_first=cover_first, verbose=verbose)
51+
configuration=configuration, cover_first=cover_first, verbose=verbose, timeout=timeout)
5052

5153
return r.to_pdf(output_path)
5254

5355

5456
def from_string(input, output_path=None, options=None, toc=None, cover=None, css=None,
55-
configuration=None, cover_first=False, verbose=False):
57+
configuration=None, cover_first=False, verbose=False, timeout=None):
5658
"""
5759
Convert given string or strings to PDF document
5860
@@ -65,12 +67,13 @@ def from_string(input, output_path=None, options=None, toc=None, cover=None, css
6567
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
6668
:param cover_first: (optional) if True, cover always precedes TOC
6769
:param verbose: (optional) By default '--quiet' is passed to all calls, set this to False to get wkhtmltopdf output to stdout.
70+
:param timeout: (optional) if passed, uses this timeout value (in seconds) for the wkhtmltopdf command
6871
6972
Returns: True on success
7073
"""
7174

7275
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css,
73-
configuration=configuration, cover_first=cover_first, verbose=verbose)
76+
configuration=configuration, cover_first=cover_first, verbose=verbose, timeout=timeout)
7477

7578
return r.to_pdf(output_path)
7679

pdfkit/pdfkit.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __str__(self):
3232
return self.msg
3333

3434
def __init__(self, url_or_file, type_, options=None, toc=None, cover=None,
35-
css=None, configuration=None, cover_first=False, verbose=False):
35+
css=None, configuration=None, cover_first=False, verbose=False, timeout=None):
3636

3737
self.source = Source(url_or_file, type_)
3838
self.configuration = (Configuration() if configuration is None
@@ -57,6 +57,7 @@ def __init__(self, url_or_file, type_, options=None, toc=None, cover=None,
5757
self.verbose = verbose
5858
self.css = css
5959
self.stylesheets = []
60+
self.timeout = timeout
6061

6162
def _genargs(self, opts):
6263
"""
@@ -187,7 +188,13 @@ def to_pdf(self, path=None):
187188
else:
188189
input = None
189190

190-
stdout, stderr = result.communicate(input=input)
191+
kwargs = {"input": input}
192+
193+
# Check if a timeout was specified and pass it along if so
194+
if self.timeout:
195+
kwargs["timeout"] = self.timeout
196+
197+
stdout, stderr = result.communicate(**kwargs)
191198
stderr = stderr or stdout or b""
192199
stderr = stderr.decode('utf-8', errors='replace')
193200
exit_code = result.returncode

0 commit comments

Comments
 (0)