Skip to content

Commit cc57600

Browse files
committed
html2print: minor cleanups on windows
- add entrypoint.sh to .gitattributes so that it keeps the line-endings (windows/docker)
1 parent 660ef53 commit cc57600

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
entrypoint.sh -text

.github/workflows/ci-windows.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
shell: powershell
2929

3030
- name: Check Chrome Version
31-
run: '& "C:\Program Files\Google\Chrome\Application\chrome.exe" --version'
31+
run: '(Get-Item "C:\Program Files\Google\Chrome\Application\chrome.exe").VersionInfo'
3232
shell: powershell
3333

3434
- name: Add Chrome to PATH
@@ -38,10 +38,6 @@ jobs:
3838
echo "$chromePath" | Out-File -Append -Encoding utf8 $env:GITHUB_PATH
3939
shell: powershell
4040

41-
- name: Verify Chrome Installation
42-
run: chrome --version
43-
shell: powershell
44-
4541
- name: Upgrade pip
4642
run: |
4743
python -m pip install --upgrade pip

html2print/html2print.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
from time import sleep
1414
from typing import Dict, List, Optional
1515

16+
import browsers
1617
import requests
17-
from requests import Response
18+
from requests import Response, RequestException
1819
from selenium import webdriver
1920
from selenium.webdriver.chrome.options import Options
2021
from selenium.webdriver.chrome.service import Service
@@ -180,6 +181,8 @@ def send_http_get_request(url, params=None, **kwargs) -> Response:
180181
f"html2print: "
181182
f"failed to get response for URL: {url} with error: {last_error}"
182183
)
184+
raise RequestException(f"GET request failed after 3 attempts: {url}") from last_error
185+
183186

184187
@staticmethod
185188
def get_chrome_version():
@@ -332,7 +335,9 @@ def create_webdriver(
332335
# Doesn't seem to be needed.
333336
# webdriver_options.add_argument('--disable-gpu') # noqa: ERA001
334337
webdriver_options.add_argument("--disable-extensions")
335-
webdriver_options.add_argument("--headless=chrome")
338+
# Use --headless=new, as it seems to be more stable on Windows (available since Chrome 109).
339+
# see https://www.selenium.dev/blog/2023/headless-is-going-away/
340+
webdriver_options.add_argument("--headless=new")
336341
# FIXME: This is not nice but otherwise it does not work in Ubuntu 24-based Docker image.
337342
# https://github.com/SeleniumHQ/selenium/issues/15327#issuecomment-2689287561
338343
webdriver_options.add_argument("--no-sandbox")
@@ -447,10 +452,18 @@ def main():
447452
args.cache_dir if args.cache_dir is not None else DEFAULT_CACHE_DIR
448453
)
449454

450-
path_to_chrome = ChromeDriverManager().get_chrome_driver(
455+
path_to_chrome_driver = ChromeDriverManager().get_chrome_driver(
451456
path_to_cache_dir
452457
)
453-
print(f"html2print: ChromeDriver available at path: {path_to_chrome}") # noqa: T201
458+
print( # noqa: T201
459+
f"html2print: ChromeDriver available at path: {path_to_chrome_driver}"
460+
)
461+
462+
if browsers.get("chrome") is not None:
463+
path_to_chrome_browser = browsers.get("chrome")["path"]
464+
print( # noqa: T201
465+
f"html2print: Chrome available at path:{path_to_chrome_browser}"
466+
)
454467
sys.exit(0)
455468

456469
elif args.command == "print":

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ dependencies = [
5454

5555
# requests is used by HTML2PDF_HTTPClient.
5656
"requests",
57+
58+
# to detect the browers path (needed on windows)
59+
"pybrowsers",
5760
]
5861

5962
[project.optional-dependencies]

0 commit comments

Comments
 (0)