Skip to content

Commit

Permalink
Merge pull request #162 from vgalin/develop
Browse files Browse the repository at this point in the history
html2image v2.0.5
  • Loading branch information
vgalin authored Sep 9, 2024
2 parents 7086f03 + 3cfb9eb commit 279b8e8
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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!

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

## Principle

Expand Down
11 changes: 8 additions & 3 deletions html2image/browsers/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def _find_chrome(user_given_executable=None):
'chromium',
'chromium-browser',
'chrome',
'google-chrome'
'google-chrome',
'google-chrome-stable'
]

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

def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False):
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging)
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False, use_new_headless=False,):
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging, use_new_headless=use_new_headless)

@property
def executable(self):
Expand Down
31 changes: 29 additions & 2 deletions html2image/browsers/chromium.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@
import subprocess

class ChromiumHeadless(Browser):
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False):
"""
Chrome/Chromium browser wrapper.
Parameters
----------
- `executable` : str, optional
+ Path to a chrome executable.
- `flags` : list of str
+ Flags to be used by the headless browser.
+ Default flags are :
- '--default-background-color=00000000'
- '--hide-scrollbars'
- `print_command` : bool
+ Whether or not to print the command used to take a screenshot.
- `disable_logging` : bool
+ Whether or not to disable Chrome's output.
- `use_new_headless` : bool, optional
+ Whether or not to use the new headless mode.
+ By default, the old headless mode is used.
+ You can also keep the original behavior to backward compatibility by setting this to `None`.
"""

def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False, use_new_headless=False,):
self.executable = executable
if not flags:
self.flags = [
Expand All @@ -16,6 +38,7 @@ def __init__(self, executable=None, flags=None, print_command=False, disable_log

self.print_command = print_command
self.disable_logging = disable_logging
self.use_new_headless = use_new_headless

def screenshot(
self,
Expand Down Expand Up @@ -58,9 +81,13 @@ def screenshot(

# command used to launch chrome in
# headless mode and take a screenshot
headless_mode = '--headless'
if self.use_new_headless is not None:
headless_mode += '=new' if self.use_new_headless else '=old'

command = [
f'{self.executable}',
'--headless',
f'{headless_mode}',
f'--screenshot={os.path.join(output_path, output_file)}',
f'--window-size={size[0]},{size[1]}',
*self.flags,
Expand Down
11 changes: 8 additions & 3 deletions html2image/browsers/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,23 @@ class EdgeHeadless(ChromiumHeadless):
----------
- `executable` : str, optional
+ Path to a edge executable.
- `flags` : list of str
+ Flags to be used by the headless browser.
+ Default flags are :
- '--default-background-color=00000000'
- '--hide-scrollbars'
- `print_command` : bool
+ Whether or not to print the command used to take a screenshot.
- `disable_logging` : bool
+ Whether or not to disable Chrome's output.
- `use_new_headless` : bool, optional
+ Whether or not to use the new headless mode.
+ By default, the old headless mode is used.
+ You can also keep the original behavior to backward compatibility by setting this to `None`.
"""

def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False):
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging)
def __init__(self, executable=None, flags=None, print_command=False, disable_logging=False, use_new_headless=False,):
super().__init__(executable=executable, flags=flags, print_command=print_command, disable_logging=disable_logging, use_new_headless=use_new_headless)

@property
def executable(self):
Expand Down
3 changes: 2 additions & 1 deletion html2image/browsers/search_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ def find_chrome(user_given_executable=None):
'chromium',
'chromium-browser',
'chrome',
'google-chrome'
'google-chrome',
'google-chrome-stable'
]

for chrome_command in chrome_commands:
Expand Down
7 changes: 5 additions & 2 deletions html2image/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def size_type(string):
parser.add_argument('--chrome_path', required=False)
# parser.add_argument('--firefox_path', required=False)
parser.add_argument('--temp_path', required=False)
parser.add_argument('--custom_flags', required=False)

args = parser.parse_args()

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

if args.custom_flags:
hti.browser.flags = args.custom_flags

if args.temp_path:
hti.temp_path = args.temp_path

paths = hti.screenshot(
html_file=args.html, css_file=args.css, other_file=args.other,
url=args.url, save_as=args.save_as, size=args.size,
browser_executable=args.chrome_path,
url=args.url, save_as=args.save_as, size=args.size
)

if not args.quiet:
Expand Down
1 change: 1 addition & 0 deletions html2image/html2image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'chrome': chrome.ChromeHeadless,
'chromium': chrome.ChromeHeadless,
'google-chrome': chrome.ChromeHeadless,
'google-chrome-stable': chrome.ChromeHeadless,
'googlechrome': chrome.ChromeHeadless,
'edge': edge.EdgeHeadless,
'chrome-cdp': chrome_cdp.ChromeCDP,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "html2image"
version = "2.0.4.3"
version = "2.0.5"
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."
authors = ["vgalin"]
license = "MIT"
Expand Down

0 comments on commit 279b8e8

Please sign in to comment.