How do I convert my entire div data into image and save it into directory without using canvas?

i have already created my data (template-containing image,text,label etc) inside div now i want to convert it into image format. is there any technique to convert a specific div content into images without using canvas.anybody plz help me !
i want to convert entire “mydiv” content into image and save that image into my image directory, when i click on save ?

    <label> Template Design</label>
    <div id="mydiv">
      <label> Good Morning</label>
      <img src="mug.png" id="img1" height="100" width="100"  />
    <input name="save" type="button" value="SAVE" />

Solution #1:

UPDATE (March 2018): Hello people of the future! This answer still gets a lot of traffic and I noticed that the old JSFiddle I’d put together was broken so it’s been updated. New JSFiddle showing this technique is here:

You might want to look into a Flash-based solution if you can’t use <canvas> (though unless this specifically needs to work in old versions of IE I’m not sure why you can’t). is an emulation of <canvas> using Flash that might get you where you need to go. The documentation says that it supports toDataURL() so that might work for you.

Can you provide more insight into what your restrictions around <canvas> are and what you’ve attempted to try already?


According to your comment below you might be able to use <canvas>, in which case you can try using – it’s a JavaScript solution that basically re-writes the DOM of a specified part of your code to a <canvas> and then allows you to interact with it however you want, including turning it into image data via toDataURL().

I’ve not used it before, but your JavaScript code would look something like this:

html2canvas([document.getElementById('mydiv')], {
    onrendered: function(canvas) {
       var data = canvas.toDataURL('image/png');
       // AJAX call to send `data` to a PHP file that creates an image from the dataURI string and saves it to a directory on the server

I’ve knocked together a quick jsfiddle of this in action here: (note: updated link as per above)

This jsfiddle shows how to use the toDataURL() method to convert the canvas to an image and append it to the document. Saving the generated image to a server is an infinitely more complex task as it will require an AJAX call or somesuch to send the image data to a PHP script that converts the generated data: URL to an actual image and then saves it to a directory that you have permission to write to. If that’s what you need to do, rather than going into that here I’d recommend starting with this Stack Overflow question: How to save an HTML5 Canvas as an image on a server?

Respondent: Scott

Solution #2:

This works perfectly…must be the simplest solution .


<div id="mydiv" style="background-image:url(Koala.jpg) ;background-size: 100%;
background-size :200px 200px;
background-repeat: no-repeat;">
<img src="mug.png" height="100" width="100"/></div>
<div id="canvas">

 <div style="width:200px; float:left" id="image">
 <p style="float:left">Image: </p>
 <div style="float:left;margin-top: 120px;" class="return-data">
 <script src=""></script>
 <script src="//"></script>


#mydiv {
background-color: lightblue;
width: 200px;
height: 200px


  <script language="javascript">
    html2canvas([document.getElementById('mydiv')], {
    onrendered: function (canvas) {
    var data = canvas.toDataURL('image/png');
     //display 64bit imag
     var image = new Image();
    image.src = data;
    // AJAX call to send `data` to a PHP file that creates an PNG image from the dataURI string and saves it to a directory on the server

    var file= dataURLtoBlob(data);

// Create new form data
var fd = new FormData();
fd.append("imageNameHere", file);

   url: "uploadFile.php",
   type: "POST",
   data: fd,
   processData: false,
   contentType: false,

    $(".return-data").html("Uploaded Canvas image link: <a href="">""</a>").hide().fadeIn("fast");


function dataURLtoBlob(dataURL) {
// Decode the dataURL    
var binary = atob(dataURL.split(',')[1]);
// Create 8-bit unsigned array
var array = [];
for(var i = 0; i < binary.length; i++) {
// Return our Blob object
return new Blob([new Uint8Array(array)], {type: 'image/png'});


here is a sample demo

Respondent: luckyamit

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.