Skip to content

A 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.

License

Notifications You must be signed in to change notification settings

vgalin/html2image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyPI PyPI PyPI GitHub GitHub

PyPI Package GitHub Repository

A lightweight Python package acting as wrapper around the headless mode of existing web browsers, allowing image generation from HTML/CSS strings, files and URLs.

 

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

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 testing 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.

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 the creation of images from simple strings.

For more information about headless modes :

Installation

HTML2Image is published on PyPI and can be installed through pip:

pip install --upgrade html2image

Or with uv:

uv pip install html2image

In addition to this package, at least one of the following browsers must be installed on your machine :

  • Google Chrome (Windows, MacOS)
  • Chromium Browser (Linux)
  • Microsoft Edge

Usage

First, import the package and instantiate it

from html2image import Html2Image
hti = Html2Image()

Multiple arguments can be passed to the constructor:

  • browser : Browser that will be used, can be set to 'chrome' (default) or 'edge'.
  • browser_executable : The path or the command that can be used to find the executable of a specific browser.
  • output_path : Path to the folder to which taken screenshots will be outputted. Default is the current working directory of your python program.
  • size : 2-Tuple representing the size of the screenshots that will be taken. Default value is (1920, 1080).
  • 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.
  • keep_temp_files : Pass True to this argument to not automatically remove temporary files created in temp_path. Default is False.

Example:

hti = Html2Image(size=(500, 200))

You can also change these values later:

hti.size = (500, 200)

Then take a screenshot

The screenshot method is the basis of this package. Most of the time, you won't need to use anything else. It can take screenshots of various things:

  • URLs via the url parameter;
  • HTML and CSS files via the html_file and css_file parameters;
  • HTML and CSS strings via the html_str and css_str parameters;
  • and "other" types of files via the other_file parameter (try it with .svg files!).

And you can also (optional):

  • Change the size of the screenshots using the size parameter;
  • Save the screenshots as a specific name using the save_as parameter.

N.B.: The screenshot method returns a list containing the path(s) of the screenshot(s) it took.

A few examples

  • URL to image
hti.screenshot(url='https://www.python.org', save_as='python_org.png')
  • HTML & CSS strings to image
html = """<h1> An interesting title </h1> This page will be red"""
css = "body {background: red;}"

hti.screenshot(html_str=html, css_str=css, save_as='red_page.png')
  • HTML & CSS files to image
hti.screenshot(
    html_file='blue_page.html', css_file='blue_background.css',
    save_as='blue_page.png'
)
  • Other files to image
hti.screenshot(other_file='star.svg')
  • Change the screenshots' size
hti.screenshot(other_file='star.svg', size=(500, 500))

Click to show all the images generated with all the code above sample_url_to_img.png sample_strings_to_img sample_files_to_img sample_other_to_img sample_other_50_50

  • Change the directory to which the screenshots are saved
hti = Html2Image(output_path='my_screenshot_folder')

OR

hti.output_path = 'my_screenshot_folder'

N.B. : the output path will be changed for all future screenshots.


Use lists in place of any parameters while using the screenshot method

  • Screenshot multiple objects using only one filename, or one filename per file:
# create three files from one filename
hti.screenshot(html_str=['A', 'B', 'C'], save_as='ABC.png')
# outputs ABC_0.png, ABC_1.png, ABC_2.png

# create three files from from different filenames
hti.screenshot(html_str=['A', 'B', 'C'], save_as=['A.png', 'B.png', 'C.png'])
# outputs A.png, B.png, C.png
  • Take multiple screenshots with the same size
# take four screenshots with a resolution of 100*50
hti.screenshot(
    html_str=['A', 'B', 'C', 'D'],
    size=(100, 50)
)
  • Take multiple screenshots with different sizes
# take four screenshots with different resolutions from three given sizes
hti.screenshot(
    html_str=['A', 'B', 'C', 'D'],
    size=[(100, 50), (100, 100), (50, 50)]
)
# respectively 100*50, 100*100, 50*50, 50*50
# if not enough sizes are given, the last size in the list will be repeated
  • Apply CSS string(s) to multiple HTML string(s)
# screenshot two html strings and apply css strings on both
hti.screenshot(
    html_str=['A', 'B'],
    css_str='body {background: red;}'
)

# screenshot two html strings and apply multiple css strings on both
hti.screenshot(
    html_str=['A', 'B'],
    css_str=['body {background: red;}', 'body {font-size: 50px;}']
)

# screenshot one html string and apply multiple css strings on it
hti.screenshot(
    html_str='A',
    css_str=['body {background: red;}', 'body {font-size: 50px;}']
)

  • Retrieve the path of the generated file(s)
    The screenshot method returns a list containing the path(s) of the screenshot(s):
paths = hti.screenshot(
    html_str=['A', 'B', 'C'],
    save_as="letters.png",
)

print(paths)
# >>> ['D:\\myFiles\\letters_0.png', 'D:\\myFiles\\letters_1.png', 'D:\\myFiles\\letters_2.png']

Change browser flags

In some cases, you may need to change the flags that are used to run the headless mode of a browser.

Flags can be used to:

  • Change the default background color of the pages;
  • Hide the scrollbar;
  • Add delay before taking a screenshot;
  • Allow you to use Html2Image when you're root, as you will have to specify the --no-sandbox flag;

You can find the full list of Chrome / Chromium flags here.

There are two ways to specify custom flags:

# At the object instanciation
hti = Html2image(custom_flags=['--my_flag', '--my_other_flag=value'])

# Afterwards
hti.browser.flags = ['--my_flag', '--my_other_flag=value']
  • Flags example use-case: adding a delay before taking a screenshot

With Chrome / Chromium, screenshots are fired directly after there is no more "pending network fetches", but you may sometimes want to add a delay before taking a screenshot, to wait for animations to end for example. There is a flag for this purpose, --virtual-time-budget=VALUE_IN_MILLISECONDS. You can use it like so:

hti = Html2Image(
    custom_flags=['--virtual-time-budget=10000', '--hide-scrollbars']
)

hti.screenshot(url='http://example.org')
  • Default flags

For ease of use, some flags are set by default. However default flags are not used if you decide to specify custom_flags or change the value of browser.flags:

# Taking a look at the default flags
>>> hti = Html2Image()
>>> hti.browser.flags
['--default-background-color=000000', '--hide-scrollbars']

# Changing the value of browser.flags gets rid of the default flags.
>>> hti.browser.flags = ['--1', '--2']
>>> hti.browser.flags
['--1', '--2'] 

# Using the custom_flags parameter gets rid of the default flags.
>>> hti = Html2Image(custom_flags=['--a', '--b'])
>>> hti.browser.flags
['--a', '--b']

Using the CLI

HTML2image comes with a Command Line Interface which you can use to generate screenshots from files and URLs on the go. You can call it by typing hti or html2image into a terminal.

Example Usage (quick start):
Screenshot a URL with a specific output name and size:

hti --url https://example.com --save-as example_page.png --size 1280,720

Screenshot multiple HTML files, applying a common CSS file, and saving with custom names:

hti --html-file page1.html page2.html --css-file common_styles.css --save-as shot1.jpg shot2.jpg

Screenshot an HTML string with a custom browser flags and verbose output:

hti --html-string "<h1>Test</h1><p>Content</p>" --custom-flags '--no-sandbox' -v

Html2Image Instance Configuration:

These arguments configure the underlying Html2Image instance.

Argument Description Example
-h, --help Show the help message and exit. hti --help
-o, --output-path PATH Directory to save screenshots. (Default: current working directory) hti --url example.com -o my_images/
--browser BROWSER Browser to use. Choices: chrome, chromium, google-chrome, google-chrome-stable, googlechrome, edge, chrome-cdp, chromium-cdp. (Default: chrome) hti --url example.com --browser edge
--browser-executable EXECUTABLE_PATH Path to the browser executable. Auto-detected if not provided. hti --browser-executable /usr/bin/google-chrome-stable
--cdp-port PORT CDP port for CDP-enabled browsers (e.g., chrome-cdp). (Default: library-dependent) hti --browser chrome-cdp --cdp-port 9222 --url example.com
--temp-path TEMP_DIR_PATH Directory for temporary files. (Default: system temp directory in an html2image subfolder) hti --html-file page.html --temp-path /my/tmp
--keep-temp-files Do not delete temporary files after screenshot generation. hti --html-file page.html --keep-temp-files
--custom-flags [FLAG ...] Custom flags to pass to the browser (e.g., '--no-sandbox' '--disable-gpu'). If provided, these flags will be used. hti --url example.com --custom-flags '--no-sandbox' '--disable-gpu'
hti --url example.com --custom-flags '--no-sandbox --disable-gpu'

Screenshot Sources:

Specify what content to screenshot. At least one source type is required.

Argument Description Example
-U, --url [URL ...] URL(s) to screenshot. hti -U https://python.org https://example.com
--html-file [FILE ...] HTML file(s) to screenshot. hti --html-file mypage.html another.html
--html-string [STRING ...] HTML string(s) to screenshot. hti --html-string "<h1>Hello</h1>" "<h2>World</h2>"
--css-file [FILE ...] CSS file(s) to load. Used by HTML files or applied with HTML strings. hti --html-file page.html --css-file style1.css style2.css
--css-string [STRING ...] CSS string(s) to apply. Combined and used with HTML strings. hti --html-string "<h1>Hi</h1>" --css-string "body{color:red;}" "h1{font-size:40px;}"
-O, --other-file [FILE ...] Other file(s) to screenshot (e.g., SVG). hti -O star.svg

Screenshot Output Options:

Control how the screenshots are saved.

Argument Description Example
-S, --save-as [FILENAME ...] Filename(s) for output images. If not provided or fewer names than items, names are auto-generated (e.g., screenshot.png, screenshot_0.png). hti -U python.org example.com -S py.png ex.png
-s, --size [W,H ...] Size(s) for screenshots as Width,Height. If one W,H pair is given, it applies to all screenshots. If multiple W,H pairs are given, they apply to corresponding screenshots sequentially; if fewer pairs than items, the last pair is repeated. If omitted, the library's default (1920,1080) is used. Width and height must be positive integers. hti -U python.org --size 800,600
hti -U python.org example.com -s 800,600 1024,768

General Options:

Argument Description Example
-q, --quiet Suppress informational output from html2image library (sets disable_logging=True). hti -U python.org -q
-v, --verbose Enable verbose output, including browser commands if supported by the browser handler. hti -U python.org -v

Using a Docker Container

You can also test the package and the CLI without having to install everything on your local machine, via a Docker container.

  • First git clone this repo
  • cd inside it
  • Build the image : docker build -t html2image .
  • Run and get inside the container : docker run -it html2image /bin/bash

Inside that container, the html2image package as well as chromium are installed.

You can load and execute a python script to use the package, or simply use the CLI.

On top of that, you can also use volumes to bind a container directory to your local machine directory, allowing you to retrieve the generated images, or even load some resources (HTML, CSS or Python files).

FAQ

  • Can I automatically take a full page screenshot?
    Sadly no, it is not easily possible. Html2Image relies on the headless mode of Chrome/Chromium browsers to take screenshots and there is no way to "ask" for a full page screenshot at the moment. If you know a way (by estimating the page size for example), please open an issue or a discussion!

  • Can I add delay before taking a screenshot?
    Yes you can, please take a look at the Change browser flags section of the readme.

  • Can I speed up the screenshot-taking process?
    Yes, when you are taking a lot of screenshots, you can achieve better performance using Parallel Processing or Multiprocessing methods. You can find an example of it here.

  • Can I make a cookie modal disappear?
    Yes and no. No, because there is no options to do it magically and extensions are not supported in headless Chrome (The I don't care about cookies extension would have been useful in this case). Yes, because you can make any element of a page disappear by retrieving its source code, modifying it as you wish, and finally screenshotting the modified source code.

Contributing and Local Development

If you want to contribute to html2image or run tests locally, follow these steps:

  1. Clone the repository:

    git clone https://github.com/vgalin/html2image.git
    cd html2image
  2. Install uv by following the installation instructions on the uv GitHub page.

  3. Create and activate a virtual environment:

    uv venv
    source .venv/bin/activate  # On Linux/macOS
    # .\.venv\Scripts\Activate.ps1  # On Windows PowerShell
    # .\.venv\Scripts\activate.bat  # On Windows CMD
  4. Install dependencies (including development tools):

    uv pip install -e ".[dev]"

    This installs the package in editable mode along with all dependencies needed for testing and linting.

Running Tests

Only basic testing is available at the moment. To run the test suite:

pytest

Or, using uv's runner:

uv run pytest

TODO List

  • A nice CLI (currently in a WIP state).
  • Support for other browsers, such as Firefox, once their screenshot feature becomes operational.
  • PDF generation?
  • Issue templates, pull request template, code of conduct.

If you see any typos or notice things that are oddly said, feel free to create an issue or a pull request.