How to Convert HTML to PNG Using JavaScript?

Estimated read time 2 min read

Converting HTML to PNG using JavaScript is typically done using a library, as there is no built-in way to do this in the browser. One library that can be used for this purpose is html2canvas.

Here is an example of how to use html2canvas to convert an HTML element to a PNG image:

<script src=""></script>

  function convertToPNG() {
    var element = document.getElementById("my-element");
    html2canvas(element).then(function(canvas) {

<button onclick="convertToPNG()">Convert to PNG</button>

<div id="my-element">
  <p>Hello, World!</p>

In this example, the convertToPNG function uses the html2canvas method to convert the HTML element with an ID of "my-element" to a canvas element. The canvas element is then appended to the document body. When the button is clicked, the convertToPNG function is executed and the HTML is converted to a PNG image.

Note that html2canvas has some limitations, such as limited support for CSS, so it may not work in all cases. It’s a good idea to check the library’s documentation and examples for more information on how to use it effectively.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply