Skip to content

Commit e8869ed

Browse files
authored
Add a timeout option: --page-load-timeout (#21)
1 parent a40f5d6 commit e8869ed

File tree

4 files changed

+713
-3
lines changed

4 files changed

+713
-3
lines changed

html2print/html2print.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ class Done(Exception):
302302
return data
303303

304304

305-
def create_webdriver(chromedriver: Optional[str], path_to_cache_dir: str):
305+
def create_webdriver(
306+
chromedriver: Optional[str], path_to_cache_dir: str, page_load_timeout: int
307+
) -> webdriver.Chrome:
306308
print("html2print: creating ChromeDriver service.", flush=True) # noqa: T201
307309
if chromedriver is None:
308310
path_to_chrome = ChromeDriverManager().get_chrome_driver(
@@ -342,7 +344,7 @@ def create_webdriver(chromedriver: Optional[str], path_to_cache_dir: str):
342344
options=webdriver_options,
343345
service=service,
344346
)
345-
driver.set_page_load_timeout(60)
347+
driver.set_page_load_timeout(page_load_timeout)
346348

347349
return driver
348350

@@ -397,6 +399,19 @@ def main():
397399
type=str,
398400
help="Optional path to a cache directory whereto the ChromeDriver is downloaded.",
399401
)
402+
command_parser_print.add_argument(
403+
"--page-load-timeout",
404+
type=int,
405+
default=2 * 60,
406+
# 10 minutes should be enough to print even the largest documents.
407+
choices=range(0, 10 * 60),
408+
help=(
409+
"How long shall HTML2Print wait while the Chrome Driver is printing "
410+
"a given HTML page to PDF. "
411+
"This is mainly driven by the time it takes for Chrome to open an "
412+
"HTML file, load it, and let HTML2PDF.js finish its job."
413+
),
414+
)
400415
command_parser_print.add_argument(
401416
"paths", nargs="+", help="Paths to input HTML file."
402417
)
@@ -418,10 +433,14 @@ def main():
418433
elif args.command == "print":
419434
paths: List[str] = args.paths
420435

436+
page_load_timeout: int = args.page_load_timeout
437+
421438
path_to_cache_dir = (
422439
args.cache_dir if args.cache_dir is not None else DEFAULT_CACHE_DIR
423440
)
424-
driver = create_webdriver(args.chromedriver, path_to_cache_dir)
441+
driver = create_webdriver(
442+
args.chromedriver, path_to_cache_dir, page_load_timeout
443+
)
425444

426445
@atexit.register
427446
def exit_handler():

0 commit comments

Comments
 (0)