Generate an image of a div and Save as
Asked Answered
B

5

26

I'd like to create an input button "Save image" that :

  1. take a screen shot of a div
  2. ask to "Save as" on the user's computer

I've found how to create a screen of a dive using html2canvas and to open it in a new tab, it works perfectly :

function printDiv2(div)
{
    html2canvas((div), {
        onrendered: function(canvas) {
            var img = canvas.toDataURL();
            window.open(img);
      }
    });
}

But for thee Save as part, is a kind of the tough part... I've found interesting topics, as I'm new in JS (and object) coding, I'm a little bit confused... I think I'll have to use the FileSaver.js and to create a new blob http://eligrey.com/blog/post/saving-generated-files-on-the-client-side/

But I don't get how to implement the saveAs in my html2canvas, how to cast properly a new blob...

function printDiv2(div)
{
    html2canvas((div), {
        onrendered: function(canvas) {
            var img = canvas.toDataURL();
            window.open(img);

            var blob = new Blob(img, {type: "image/jpeg"});
            var filesaver = saveAs(blob, "my image.png");
      }
    });
}

Also I tried to do something with this, by extracting the base64 generated URL, but it's too complicated for me to understand everyting : http://bl.ocks.org/nolanlawson/0eac306e4dac2114c752

But someone give me a few tips and help me please ?

Belleslettres answered 12/11, 2015 at 9:53 Comment(5)
How you had taken the screenshot? provide the code if you are having that oneEsperanto
Answer is bellow :-)Belleslettres
#45035986Esperanto
If you are using the latest version of the library then syntax has been changed.Nannette
You can use canvas.toBlob(): developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/…Rheumatic
B
7

Here is the final code, if it can helps you :

function PrintDiv(div)
{
    html2canvas((div), {
        onrendered: function(canvas) {
            var myImage = canvas.toDataURL();
            downloadURI(myImage, "MaSimulation.png");
      }
    });
}

function downloadURI(uri, name) {
    var link = document.createElement("a");

    link.download = name;
    link.href = uri;
    document.body.appendChild(link);
    link.click();   
    //after creating link you should delete dynamic link
    //clearDynamicLink(link); 
}
Belleslettres answered 10/7, 2017 at 5:1 Comment(2)
Working in chrome but not in IE 11Saragossa
Can you please share the full code or explain a bit more I don´t fully undestand how it works and I'm trying to achieve the same thingElectrokinetics
E
18

You could do this approach:

//Creating dynamic link that automatically click
function downloadURI(uri, name) {
    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    link.click();
    //after creating link you should delete dynamic link
    //clearDynamicLink(link); 
}

//Your modified code.
function printToFile(div) {
    html2canvas(div, {
        onrendered: function (canvas) {
            var myImage = canvas.toDataURL("image/png");
            //create your own dialog with warning before saving file
            //beforeDownloadReadMessage();
            //Then download file
            downloadURI("data:" + myImage, "yourImage.png");
        }
    });
}
Erinn answered 12/11, 2015 at 10:26 Comment(3)
Yesssssss !! Just needed to add document.body.appendChild(link); after the link.href = uri; and it works !! In a real easy way, thank you.Belleslettres
Thanks! And it seems the post-rendering syntax has changed as of May'17.. it's now html2canvas(div).then(function(canvas) {...}); ref: html2canvas.hertzen.com/getting-startedSupply
html2canvas is not definedBartz
B
7

Here is the final code, if it can helps you :

function PrintDiv(div)
{
    html2canvas((div), {
        onrendered: function(canvas) {
            var myImage = canvas.toDataURL();
            downloadURI(myImage, "MaSimulation.png");
      }
    });
}

function downloadURI(uri, name) {
    var link = document.createElement("a");

    link.download = name;
    link.href = uri;
    document.body.appendChild(link);
    link.click();   
    //after creating link you should delete dynamic link
    //clearDynamicLink(link); 
}
Belleslettres answered 10/7, 2017 at 5:1 Comment(2)
Working in chrome but not in IE 11Saragossa
Can you please share the full code or explain a bit more I don´t fully undestand how it works and I'm trying to achieve the same thingElectrokinetics
C
2

This works fine for me.

function downloadURI(uri, name) {
    var link = document.createElement("a");

    link.download = name;
    link.href = uri;
    document.body.appendChild(link);
    link.click();
    clearDynamicLink(link); 
}

function DownloadAsImage() {
    var element = $("#table-card")[0];
    html2canvas(element).then(function (canvas) {
        var myImage = canvas.toDataURL();
        downloadURI(myImage, "cartao-virtual.png");
    });
}
Carminecarmita answered 9/5, 2018 at 12:52 Comment(0)
E
1

Have you looked at

http://eligrey.com/demos/FileSaver.js/

Looks like it does what you need

Educatory answered 12/11, 2015 at 10:15 Comment(1)
Thank you, but I already went on there, that's why I know I have to use the Filesaver.js, on this page, they are using canvas, but I can't put my div into <canvas> tagBelleslettres
C
1

Nowadays html2canvas has changed to javascript promise. So the updated code should be:

function PrintDiv(div)
{
    html2canvas(div).then(canvas => {
        var myImage = canvas.toDataURL();
        downloadURI(myImage, "MaSimulation.png");
    });
}

function downloadURI(uri, name) {
    var link = document.createElement("a");

    link.download = name;
    link.href = uri;
    document.body.appendChild(link);
    link.click();   
    //after creating link you should delete dynamic link
    //clearDynamicLink(link); 
}
Cyanocobalamin answered 7/9, 2022 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.