Skip to content
Merged
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
33 changes: 28 additions & 5 deletions html2pdf4doc/html2pdf4doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from datetime import datetime
from pathlib import Path
from time import sleep, time
from typing import Dict, Iterator, List, Optional, Tuple
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

import requests
from pypdf import PdfReader
Expand Down Expand Up @@ -63,6 +63,29 @@ def extract_page_count(logs: List[Dict[str, str]]) -> int:
raise ValueError("No page count found in logs.")


class IntRange:
def __init__(self, imin: int, imax: int) -> None:
self.imin: int = imin
self.imax: int = imax

def __call__(self, arg: Any) -> Union[int, argparse.ArgumentTypeError]:
value: Optional[int]
try:
value = int(arg)
except ValueError:
value = None

if value is not None and self.imin <= value <= self.imax:
return value

raise argparse.ArgumentTypeError(
f"Must be an integer in the range [{self.imin}, {self.imax}]."
)

def __str__(self) -> str:
return f"{self.imin}-{self.imax}"


class ChromeDriverManager:
def get_chrome_driver(self, path_to_cache_dir: str) -> str:
chrome_version: Optional[str] = self.get_chrome_version()
Expand Down Expand Up @@ -486,15 +509,15 @@ def main() -> None:
)
command_parser_print.add_argument(
"--page-load-timeout",
type=int,
default=2 * 60,
# 10 minutes should be enough to print even the largest documents.
choices=range(0, 10 * 60),
type=IntRange(0, 10 * 60),
default=2 * 60,
help=(
"How long shall html2pdf4doc Python driver wait while the "
"Chrome Driver is printing a given HTML page to PDF. "
"This is mainly driven by the time it takes for Chrome to open an "
"HTML file, load it, and let HTML2PDF4Doc.js finish its job."
"HTML file, load it, and let HTML2PDF4Doc.js finish its job. "
"Allowed range: %(type)s seconds, default: %(default)s seconds."
),
)
command_parser_print.add_argument(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NOT RELEVANT FOR THIS TEST.
3 changes: 3 additions & 0 deletions tests/integration/06_page_load_timeout_validation/test.itest
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RUN: %expect_exit 2 %html2pdf print --page-load-timeout 1000000 %S/index1.html %S/Output/index1.pdf 2>&1 | filecheck %s

CHECK: html2pdf4doc.py print: error: argument --page-load-timeout: Must be an integer in the range [0, 600].
Loading