How to Convert HTML to an Image in JavaScript
If you want to take a screenshot of part or all of your web page using JavaScript, you might find yourself stuck. Creating an image from an HTML element can seem like a challenge, as there’s no direct way to do it in JavaScript.
Thankfully, this isn’t an impossible task and is, in fact, quite easy with the proper tools. Using the html-to-image library, making images of DOM nodes is as simple as a single function call.

How Does html-to-image Work?
Thehtml-to-imagelibrary produces an image in the form of a base64 data URL. It supports several output formats, including PNG, JPG, and SVG. To perform this conversion, the library uses this algorithm:
Caveats and Limitations
Even though html-to-image is a great library, it’s not perfect. It has a few caveats, namely:
Using the Library
To try the library out, the first thing you need to do is create a project directory on your local machine. Next, install html-to-image in that directory usingthe npm package manager. Here’s the terminal command to install it:
You should also install a JavaScript bundler, to make it a little easier to use the library. Theesbuildbundler can help package node modules into web-compatible scripts.
That’s all you need to install. Next, create a file calledindex.htmlin your directory, and serve it with a web server of your choice. Put the following code in index.html:
This code creates two elements on the page: a div with a gradient background, and some text and an unordered list inside another div. Next, you’ll write the JavaScript to convert these elements to images. Put the following code in a new file called script.js:

This code does a few things:
Now use esbuild to generate the bundled file (out.js) that index.html references by running the following in your terminal:

At this point, here’s what index.html should look like in your browser:
Even though the copies look exactly the same as the originals, they are actually image elements. it’s possible to confirm this byopening your dev toolsand inspecting them.

This library also works with JavaScript frameworks. Thehtml-to-image documentationcontains instructions on how to generate other image formats. It also includes an example showing how to use the library with React.
Taking Screenshots With JavaScript Is Easy
There’s no native JavaScript method for creating images from HTML elements, or taking screenshots of the DOM. However, with the help of libraries and services like html-to-image, it becomes an easy task.
There are other ways of achieving similar results, such as the wkhtmltoimage library. You can use this open-source tool to take screenshots of a complete web page.
Need to turn a webpage into a PDF for easy sharing? It’s easy with these Linux terminal tools.
Don’t let aging hardware force you into buying expensive upgrades.
The fix was buried in one tiny toggle.
You can block out the constant surveillance and restore your privacy with a few quick changes.
Lose your laptop without this feature, and you’ll wish you had turned it on.
Every squeak is your PC’s way of crying for help.