Convert HTML to Image in Jquery [Div or Table to jpg/ png]

satinder singh / Updated : / 58 Comments

jQuery HTML to Image Converter: This article explains how to convert div to an image in jquery. Yes, we are going to generate an image from our HTML page on client-side using jQuery. We can also convert a particular part of our Webpage ( C# Web project/ PHP). All we need HTML tag, and using html2canvas js library we can create images .i.e converting HTML table to Image PNG, JPG or converting DIV, UL, LI tag into Jpg image format. In short using, html2canvas will render HTML to Images which means now we can take the screenshot of Div or any HTML element of a web page.

Where the use of converting HTML to image/picture?

As we know HTML5 gives some excellent feature, for example, we have a Canvas tag on our web page. Now on HTML 5 Canvas, we draw something or let say we create a pie chart using jquery, after that, we want to save this as an Image may be in png or jpg format. We convert Canvas to Image at client-side and later save it on our server. Another example, which I have implemented in my C# web project where I had to add multiple images and then drag/shuffle all the images inside a canvas then save and upload this image using jQuery ajax, and give the print command to make Mug Sticker, Tshirt Sticker. The idea behind it is to let the user add his / her photos. Allow them to arrange or to shuffle it. Later on, save button click using Jquery HTML canvas save the image which will send for printing purpose. Now let’s head towards the coding part and see how we can convert HTML to Picture 🙂

Example: Render HTML to Image in JQuery

Step to render HTML to Image [PNG/JGP] using jQuery

  1. Download and import HTML2Canvas jquery files.
  2. Add HTML markup.
  3. jQuery Code: Button click convert HTML to Canvas.
  4. jQuery Code: Download HTML to IMAGE.

# Download and import HTML2Canvas files.

First, we need to download and import latest jquery library in our web page. You can import your server hosted jQuery file, or you can also use from the Google-hosted site (recommended). For converting HTML to Image, we need to import one more js library, and it is HTML2CANVAS download it and import into the HTML head tag. Now we are done with downloading, so our final head tag looks like as shown below.

<script src="Scripts/jquer_latest_2.11_.min.js" type="text/javascript"></script>
<script src="Scripts/html2canvas.js" type="text/javascript"></script>

# Add HTML markup (HTML element for which we create image)

Let make this article very simple, so now am adding some dummy HTML elements i.e. P tag, but you can add dynamic HTML using jQuery ajax for which you want to generate images. Now we add button tag which creates Image of specific HTML and a download button. On download button click images get downloaded on your local drive. 1st we create a canvas of our HTML elements and then download it to the local drive as a picture, all this done at client-side using jquery. HTML MARKUP:

    <div id="html-content-holder" style="background-color: #F0F0F1; color: #00cc65; width: 500px;
        padding-left: 25px; padding-top: 10px;">
        <strong></strong><hr />
        <h2 style="color: #3e4b51;">
            Html to canvas, and canvas to proper image
        <p style="color: #3e4b51;">
            <b></b> is a programming blog. Tutorials focused on Programming ASP.Net,
            C#, jQuery, AngularJs, Gridview, MVC, Ajax, Javascript, XML, MS SQL-Server, NodeJs,
            Web Design, Software</p>
        <p style="color: #3e4b51;">
            <b>html2canvas</b> script allows you to take "screenshots" of webpages or parts
            of it, directly on the users browser. The screenshot is based on the DOM and as
            such may not be 100% accurate to the real representation as it does not make an
            actual screenshot, but builds the screenshot based on the information available
            on the page.
    <input id="btn-Preview-Image" type="button" value="Preview" />
    <a id="btn-Convert-Html2Image" href="#">Download</a>
    <br />
    <h3>Preview :</h3>
    <div id="previewImage">

#Button click converts HTML to Canvas:

As you see, we have added a preview button. Now on preview button click using HTML2Canvas will render our HTML to Canvas. The code looks like as written below:

var element = $("#html-content-holder"); // global variable
var getCanvas; // global variable

    $("#btn-Preview-Image").on('click', function () {
         html2canvas(element, {
         onrendered: function (canvas) {
                getCanvas = canvas;

The above-written jquery code will create a canvas and append it to our div container for preview purpose.

Try It Yourself ⇒

# jQuery: Download HTML to IMAGE / Screenshot of Div content

Here we are ready with our canvas now we just need to download it as Image format. On download button click will convert canvas to png image format you can set any image format. Here’s the code for downloading.

$("#btn-Convert-Html2Image").on('click', function () {
    var imgageData = getCanvas.toDataURL("image/png");
    // Now browser starts downloading it instead of just showing it
    var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
    $("#btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData);
Try It Yourself ⇒


Conclusion:  Here’s an easy way to render HTML to image like PNG. As it’s easy to create an image from a canvas. But to render standard HTML elements like DIV, UL, LI, Table, P tags to an image we use HTML2Canvas Js library. Using HTML2Canvas will take the screenshot of our HTML web pages.

You can also check these articles:

  1. Preview Image before upload it with jQuery in
  2. How to get the filename, size, type count in jQuery [input file, File Api].
  3. Create dynamic HTML table using jquery.
  4. How to remove table row tr in jquery on button click.
  5. Jquery: How to disable Button, Div, Anchor tag.

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

58 comments on “Convert HTML to Image in Jquery [Div or Table to jpg/ png]”

  1. php devloper

    code is nice but allowTaint: true , using this parameter we are not be able to save image. it show error “Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported.” and without using taint parameter unable to access cross origin images like s3 images.

  2. gnanendar


    This is really working Awesome but i have question when convert to canvas my font styles are changed


  3. Shankar

    Thanks for the Excellent Tutorial. Its working great in Chrome and Firefox, but In Internet Explorer 11, Edge and Safari, I’m not able to download the image. Please help me how to solve this issue???

  4. Rajendra

    I have an HTML content that is being updated by js in bulk say 1000 iterations. I want to convert all of them to respective image and create a zip of all the images and make the zip to be downloaded.
    please kindly help.
    Thank you.

  5. Daniel

    Hey, its an excellent example, but im having a problem, i hope you can help me with this.

    Im using 3 image files (png) into the canvas, but when i try to download it, they arent rendered, i allowed the option allowTaint but im getting a console log what says Taintered canvases may not be exported. I can make the preview but i cant download, what can i do? 🙁

  6. vivek singh

    i see your code ..excellent . how my create image automatic save in my specific folder with my genret name.

  7. Vikram Singh

    Is this possible to download image directly on clicking download button without preview(with out clickig preview button)

  8. Bhuvana


    Excellent Tutorial But I have an issue in I need to download an image without previewing. As you said I have copied both button code into one function and comment this line $(“#previewImage”).append(canvas); Its working. But when clicking the download its not working . In second click only its downloading the corresponding image. Can you help me ASAP???

  9. Karthick

    Excellent Tutorial. Its working great, but I need without preview and directly download to particular div.

    kindly suggest

  10. Ghazi Anwer

    Very nicely explained. can you please help me out how to export an HTML page containing highcharts along with some HTML table into pdf. please help as I have struck in it.


  11. Maynard

    Amazing code and tutorial. Thank you so much. Has anyone tried using it live? Does it work? I thought I had found exactly what I needed and it worked on my local server, but my instructor tried to shoot me down by saying it wouldn’t work on a live server for security reasons (?).

  12. Fred

    HI there,

    I’m a big fan of your script. My problem is, the div that I would like to export is wider than the display (I have to scroll right to see the full picture).

    Issue : your script is not capturing what’s not displayed on load. Is there a way to specify the width /height that should be capture ? I saw a lot of “options” in the html2canvas.js, any user guide explaining how to use them ?

    Thanks !

  13. Azeem

    Thanks for the tutorial Satinder!

    But what if I want to download it directly without having the preview step, is that possible?

  14. Daniel

    Is it possible to change the image resolution or size of the image in this example? The problem is that when I download the image it’s very blurry and the resolution of the image is low, so when I zoom in it gets pixelated. What can I do?

  15. ronx

    Hi Satinder,

    How can I just make image download on click of download button, I don’t want user to first press preview and then download,

    I just want to click download and image download.

  16. monica

    Thanks for the explanation! Just wondering does this code works on iOS? Cause i am trying to make a mobile application where there’s this button and the button can save image and save it to camera roll.
    Many thanks!

  17. Nelson

    Excellent Tutorial. Its working great in chrome and firefox, but In safari9.1, I’m not able to download the png., it get base64. Please help me how to solve this issue….

  18. Madhu

    It’s working fine for text. But when i use images it’s not showing. Can anyone give a solution for an tag

  19. venkat

    Excellent Tutorial. Its working great in chrome and firefox, but In Internet Explorer 11 I’m not able to download the image… Please help me how to solve this issue…..

  20. venkat

    Excellent Tutorial. Its working great in chrome and firefox, but In Internet Explorer 11 I’m not able to download the image… Please help me how to solve this issue….

Post Comment

Your email address will not be published. Required fields are marked *