Skip to content

Commit 279b8e8

Browse files
authored
Merge pull request #162 from vgalin/develop
html2image v2.0.5
2 parents 7086f03 + 3cfb9eb commit 279b8e8

File tree

8 files changed

+55
-12
lines changed

8 files changed

+55
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
This package has been tested on Windows, Ubuntu (desktop and server) and MacOS. If you encounter any problems or difficulties while using it, feel free to open an issue on the GitHub page of this project. Feedback is also welcome!
2929

30+
⚠️ Disclaimer: Use this package with trusted content only. Processing untrusted or unsanitized input can lead to malicious code execution. Always ensure content safety.
3031

3132
## Principle
3233

html2image/browsers/chrome.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def _find_chrome(user_given_executable=None):
114114
'chromium',
115115
'chromium-browser',
116116
'chrome',
117-
'google-chrome'
117+
'google-chrome',
118+
'google-chrome-stable'
118119
]
119120

120121
for chrome_command in chrome_commands:
@@ -178,10 +179,14 @@ class ChromeHeadless(ChromiumHeadless):
178179
+ Whether or not to print the command used to take a screenshot.
179180
- `disable_logging` : bool
180181
+ Whether or not to disable Chrome's output.
182+
- `use_new_headless` : bool, optional
183+
+ Whether or not to use the new headless mode.
184+
+ By default, the old headless mode is used.
185+
+ You can also keep the original behavior to backward compatibility by setting this to `None`.
181186
"""
182187

183-
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False):
184-
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging)
188+
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False, use_new_headless=False,):
189+
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging, use_new_headless=use_new_headless)
185190

186191
@property
187192
def executable(self):

html2image/browsers/chromium.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,29 @@
44
import subprocess
55

66
class ChromiumHeadless(Browser):
7-
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False):
7+
"""
8+
Chrome/Chromium browser wrapper.
9+
10+
Parameters
11+
----------
12+
- `executable` : str, optional
13+
+ Path to a chrome executable.
14+
- `flags` : list of str
15+
+ Flags to be used by the headless browser.
16+
+ Default flags are :
17+
- '--default-background-color=00000000'
18+
- '--hide-scrollbars'
19+
- `print_command` : bool
20+
+ Whether or not to print the command used to take a screenshot.
21+
- `disable_logging` : bool
22+
+ Whether or not to disable Chrome's output.
23+
- `use_new_headless` : bool, optional
24+
+ Whether or not to use the new headless mode.
25+
+ By default, the old headless mode is used.
26+
+ You can also keep the original behavior to backward compatibility by setting this to `None`.
27+
"""
28+
29+
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False, use_new_headless=False,):
830
self.executable = executable
931
if not flags:
1032
self.flags = [
@@ -16,6 +38,7 @@ def __init__(self, executable=None, flags=None, print_command=False, disable_log
1638

1739
self.print_command = print_command
1840
self.disable_logging = disable_logging
41+
self.use_new_headless = use_new_headless
1942

2043
def screenshot(
2144
self,
@@ -58,9 +81,13 @@ def screenshot(
5881

5982
# command used to launch chrome in
6083
# headless mode and take a screenshot
84+
headless_mode = '--headless'
85+
if self.use_new_headless is not None:
86+
headless_mode += '=new' if self.use_new_headless else '=old'
87+
6188
command = [
6289
f'{self.executable}',
63-
'--headless',
90+
f'{headless_mode}',
6491
f'--screenshot={os.path.join(output_path, output_file)}',
6592
f'--window-size={size[0]},{size[1]}',
6693
*self.flags,

html2image/browsers/edge.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,23 @@ class EdgeHeadless(ChromiumHeadless):
150150
----------
151151
- `executable` : str, optional
152152
+ Path to a edge executable.
153-
154153
- `flags` : list of str
155154
+ Flags to be used by the headless browser.
156155
+ Default flags are :
157156
- '--default-background-color=00000000'
158157
- '--hide-scrollbars'
159158
- `print_command` : bool
160159
+ Whether or not to print the command used to take a screenshot.
160+
- `disable_logging` : bool
161+
+ Whether or not to disable Chrome's output.
162+
- `use_new_headless` : bool, optional
163+
+ Whether or not to use the new headless mode.
164+
+ By default, the old headless mode is used.
165+
+ You can also keep the original behavior to backward compatibility by setting this to `None`.
161166
"""
162167

163-
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False):
164-
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging)
168+
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False, use_new_headless=False,):
169+
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging, use_new_headless=use_new_headless)
165170

166171
@property
167172
def executable(self):

html2image/browsers/search_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ def find_chrome(user_given_executable=None):
213213
'chromium',
214214
'chromium-browser',
215215
'chrome',
216-
'google-chrome'
216+
'google-chrome',
217+
'google-chrome-stable'
217218
]
218219

219220
for chrome_command in chrome_commands:

html2image/cli.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def size_type(string):
4040
parser.add_argument('--chrome_path', required=False)
4141
# parser.add_argument('--firefox_path', required=False)
4242
parser.add_argument('--temp_path', required=False)
43+
parser.add_argument('--custom_flags', required=False)
4344

4445
args = parser.parse_args()
4546

@@ -60,13 +61,15 @@ def size_type(string):
6061
if args.chrome_path:
6162
hti.chrome_path = args.chrome_path
6263

64+
if args.custom_flags:
65+
hti.browser.flags = args.custom_flags
66+
6367
if args.temp_path:
6468
hti.temp_path = args.temp_path
6569

6670
paths = hti.screenshot(
6771
html_file=args.html, css_file=args.css, other_file=args.other,
68-
url=args.url, save_as=args.save_as, size=args.size,
69-
browser_executable=args.chrome_path,
72+
url=args.url, save_as=args.save_as, size=args.size
7073
)
7174

7275
if not args.quiet:

html2image/html2image.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'chrome': chrome.ChromeHeadless,
2222
'chromium': chrome.ChromeHeadless,
2323
'google-chrome': chrome.ChromeHeadless,
24+
'google-chrome-stable': chrome.ChromeHeadless,
2425
'googlechrome': chrome.ChromeHeadless,
2526
'edge': edge.EdgeHeadless,
2627
'chrome-cdp': chrome_cdp.ChromeCDP,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "html2image"
3-
version = "2.0.4.3"
3+
version = "2.0.5"
44
description = "Package acting as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files."
55
authors = ["vgalin"]
66
license = "MIT"

0 commit comments

Comments
 (0)