Skip to content

Commit a07ee3c

Browse files
authored
Merge pull request #121 from vgalin/develop
Develop
2 parents 3184329 + 5005ddd commit a07ee3c

File tree

18 files changed

+1115
-152
lines changed

18 files changed

+1115
-152
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
python-version: [3.6, 3.7, 3.8]
19+
python-version: [3.6, 3.7, 3.8, 3.9, "3.10"]
2020
poetry-version: [1.1.4]
2121
os: [ubuntu-18.04, macos-latest, windows-latest]
2222
runs-on: ${{ matrix.os }}
@@ -33,6 +33,7 @@ jobs:
3333
run: |
3434
python -m pip install --upgrade pip
3535
pip install flake8 pytest
36+
pip install -r requirements.txt
3637
pip install -r requirements-test.txt
3738
- name: Flake8 linting
3839
run: |

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,7 @@ dmypy.json
130130

131131
# Added
132132
.vscode/
133-
tests_output/
133+
tests_output/
134+
135+
# .lock file not relevant for libraries
136+
poetry.lock

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
FROM python
22

3-
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
3+
RUN curl -sSL https://install.python-poetry.org | python3 -
44

55
WORKDIR /pkgs/html2image
66
COPY . .
77

8-
RUN $HOME/.poetry/bin/poetry install
9-
RUN $HOME/.poetry/bin/poetry build
8+
RUN $HOME/.local/bin/poetry install
9+
RUN $HOME/.local/bin/poetry build
1010
RUN pip install dist/*.whl
1111

1212
RUN apt-get update -y && apt-get install -y chromium
@@ -19,4 +19,4 @@ RUN echo 'export CHROMIUM_FLAGS="$CHROMIUM_FLAGS --no-sandbox"' >> /etc/chromium
1919
# MOTD
2020
RUN echo " \n =============HTML2IMAGE============= \n Welcome to the html2image CLI container ! \n Type html2image -h for help :)" >> /etc/motd
2121
RUN echo "clear" >> /root/.bashrc
22-
RUN echo "cat /etc/motd" >> /root/.bashrc
22+
RUN echo "cat /etc/motd" >> /root/.bashrc

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1-
<img src="readme_assets/html2image_black.png" align="right" alt="html2image logo" title="html2image" height="128"/>
21

3-
# HTML2Image
4-
[
2+
3+
4+
<h1 align="center">
5+
<a href="https://github.com/MadeInPierre/finalynx">
6+
<img src="readme_assets/html2image_black.png" alt="html2image logo" title="html2image" height="200"/>
7+
</a>
8+
</h1>
9+
10+
11+
<div align="center">
12+
513
![PyPI](https://img.shields.io/pypi/v/html2image.svg)
614
![PyPI](https://img.shields.io/pypi/pyversions/html2image.svg)
715
![PyPI](https://img.shields.io/github/license/vgalin/html2image.svg)
8-
](https://pypi.org/project/html2image/)
9-
[
1016
![GitHub](https://img.shields.io/github/v/release/vgalin/html2image?include_prereleases)
1117
![GitHub](https://img.shields.io/github/languages/code-size/vgalin/html2image)
12-
](https://github.com/vgalin/html2image)
1318

14-
HTML2Image is a lightweight Python package that acts as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files.
19+
20+
|[PyPI Package](https://pypi.org/project/html2image/)|[GitHub Repository](https://github.com/vgalin/html2image)|
21+
|-|-|
22+
23+
**A lightweight Python package acting a wrapper around the headless mode of existing web browsers, allowing images generation from HTML/CSS strings, files and URLs.**
24+
25+
</div>
26+
&nbsp;
1527

1628
This package has been tested on Windows, Ubuntu (desktop and server) and MacOS. It is currently in a work in progress stage. If you encounter any problem or difficulties while using it, feel free to open an issue on the GitHub page of this project. Feedback is also welcome!
1729

1830

1931
## Principle
2032

33+
2134
Most web browsers have a Headless Mode, which is a way to run them without displaying any graphical interface. Headless mode is mainly used for automated testings but also comes in handy if you want to take screenshots of web pages that are exact replicas of what you would see on your screen if you were using the browser yourself.
2235

2336
However, for the sake of taking screenshots, headless mode is not very convenient to use. HTML2Image aims to hide the inconveniences of the browsers' headless modes while adding useful features such as allowing to create an image from as little as a string.
@@ -36,6 +49,7 @@ pip install --upgrade html2image
3649
In addition to this package, at least one of the following browsers **must** be installed on your machine :
3750
- Google Chrome (Windows, MacOS)
3851
- Chromium Browser (Linux)
52+
- Microsoft Edge
3953

4054
## Usage
4155

@@ -45,14 +59,14 @@ from html2image import Html2Image
4559
hti = Html2Image()
4660
```
4761

48-
<details>
49-
<summary> Multiple arguments can be passed to the constructor (click to expand):</summary>
62+
Multiple arguments can be passed to the constructor:
5063

51-
- `browser` : Browser that will be used, set by default to `'chrome'` (the only browser supported by HTML2Image at the moment)
64+
- `browser` : Browser that will be used, can be set to `'chrome'` (default) or `'edge'`.
5265
- `browser_executable` : The path or the command that can be used to find the executable of a specific browser.
5366
- `output_path` : Path to the folder to which taken screenshots will be outputed. Default is the current working directory of your python program.
5467
- `size` : 2-Tuple representing the size of the screenshots that will be taken. Default value is `(1920, 1080)`.
5568
- `temp_path` : Path that will be used to put together different resources when screenshotting strings of files. Default value is `%TEMP%/html2image` on Windows, and `/tmp/html2image` on Linux and MacOS.
69+
- `keep_temp_files` : Pass True to this argument to not automatically remove temporary files created in `temp_path`. Default is False.
5670

5771
Example:
5872
```python
@@ -63,8 +77,6 @@ You can also change these values later:
6377
``` python
6478
hti.size = (500, 200)
6579
```
66-
</details>
67-
<br>
6880

6981
### Then take a screenshot
7082

html2image/browsers/browser.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class Browser(ABC):
55
"""Abstract class representing a web browser."""
66

7-
def __init__(self, flags):
7+
def __init__(self, flags, disable_logging):
88
pass
99

1010
@property
@@ -20,3 +20,24 @@ def executable(self, value):
2020
@abstractmethod
2121
def screenshot(self, *args, **kwargs):
2222
pass
23+
24+
@abstractmethod
25+
def __enter__(self):
26+
pass
27+
28+
@abstractmethod
29+
def __exit__(self, *exc):
30+
pass
31+
32+
@property
33+
@abstractmethod
34+
def disable_logging(self):
35+
pass
36+
37+
38+
class CDPBrowser(Browser):
39+
"""A web browser that can be interacted with via Chrome DevTools Protocol.
40+
"""
41+
42+
def __init__(self, flags, cdp_port, disable_logging):
43+
pass

html2image/browsers/chrome.py

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from .browser import Browser
1+
from .chromium import ChromiumHeadless
22
from .search_utils import get_command_origin, find_first_defined_env_var
33

44
import subprocess
5-
import platform
65
import os
76
import shutil
7+
import platform
88

99
ENV_VAR_LOOKUP_TOGGLE = 'HTML2IMAGE_TOGGLE_ENV_VAR_LOOKUP'
1010

@@ -15,7 +15,6 @@
1515
'CHROME_EXE',
1616
]
1717

18-
1918
def _find_chrome(user_given_executable=None):
2019
""" Finds a Chrome executable.
2120
@@ -161,8 +160,7 @@ def _find_chrome(user_given_executable=None):
161160
'machine, please specify it yourself.'
162161
)
163162

164-
165-
class ChromeHeadless(Browser):
163+
class ChromeHeadless(ChromiumHeadless):
166164
"""
167165
Chrome/Chromium browser wrapper.
168166
@@ -174,23 +172,16 @@ class ChromeHeadless(Browser):
174172
- `flags` : list of str
175173
+ Flags to be used by the headless browser.
176174
+ Default flags are :
177-
- '--default-background-color=000000'
175+
- '--default-background-color=00000000'
178176
- '--hide-scrollbars'
179177
- `print_command` : bool
180178
+ Whether or not to print the command used to take a screenshot.
179+
- `disable_logging` : bool
180+
+ Whether or not to disable Chrome's output.
181181
"""
182182

183-
def __init__(self, executable=None, flags=None, print_command=False):
184-
self.executable = executable
185-
if not flags:
186-
self.flags = [
187-
'--default-background-color=000000',
188-
'--hide-scrollbars',
189-
]
190-
else:
191-
self.flags = [flags] if isinstance(flags, str) else flags
192-
193-
self.print_command = print_command
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)
194185

195186
@property
196187
def executable(self):
@@ -199,58 +190,3 @@ def executable(self):
199190
@executable.setter
200191
def executable(self, value):
201192
self._executable = _find_chrome(value)
202-
203-
def screenshot(
204-
self,
205-
input,
206-
output_path,
207-
output_file='screenshot.png',
208-
size=(1920, 1080),
209-
):
210-
""" Calls Chrome or Chromium headless to take a screenshot.
211-
212-
Parameters
213-
----------
214-
- `output_file`: str
215-
+ Name as which the screenshot will be saved.
216-
+ File extension (e.g. .png) has to be included.
217-
+ Default is screenshot.png
218-
- `input`: str
219-
+ File or url that will be screenshotted.
220-
+ Cannot be None
221-
- `size`: (int, int), optional
222-
+ Two values representing the window size of the headless
223-
+ browser and by extention, the screenshot size.
224-
+ These two values must be greater than 0.
225-
Raises
226-
------
227-
- `ValueError`
228-
+ If the value of `size` is incorrect.
229-
+ If `input` is empty.
230-
"""
231-
232-
if not input:
233-
raise ValueError('The `input` parameter is empty.')
234-
235-
if size[0] < 1 or size[1] < 1:
236-
raise ValueError(
237-
f'Could not screenshot "{output_file}" '
238-
f'with a size of {size}:\n'
239-
'A valid size consists of two integers greater than 0.'
240-
)
241-
242-
# command used to launch chrome in
243-
# headless mode and take a screenshot
244-
command = [
245-
f'{self.executable}',
246-
'--headless',
247-
f'--screenshot={os.path.join(output_path, output_file)}',
248-
f'--window-size={size[0]},{size[1]}',
249-
*self.flags,
250-
f'{input}',
251-
]
252-
253-
if self.print_command:
254-
print(' '.join(command))
255-
256-
subprocess.run(command)

0 commit comments

Comments
 (0)