I would use the library: gift-to-image
You can read the documentation about its use here
Here comes an example:
var node = document.getElementById('my-node');
domtoimage.toPng(node)
.then (function (dataUrl) {
var img = new Image();
img.src = dataUrl;
document.body.appendChild(img); //esse método exibe a imagem na própria pagina
})
.catch(function (error) {
console.error('erro exibido caso falhe', error);
});
How do I open the image on another page?
Simple, refer to a blank page as the value of Document to be used.
How do I transform the image and start downloading it altomatically?
domtoimage.toPng(document.getElementById('my-node'), { quality: 1 })
.then(function (dataUrl) {
var link = document.createElement('a');
link.download = 'NomeDaImagem.png';
link.href = dataUrl;
link.click();
});
Document.appendchild(img); causes the image to appear on the same page? How do I open on another? Or download it automatically?
– user75204
@lucaswmolin this, the appendchild method will display the image on the page itself. You can open another by changing the url for Document and open it via JS code. To download automatically I would suggest transforming the image into a BLOB file and using a library like Filesaver (link - https://github.com/eligrey/FileSaver.js/) there are some examples in the documentation, it is quite simple.
– Paz
want me to incorporate your questions in my answer? @lucaswmolin
– Paz
Yes, please.
– user75204
@lucaswmolin edited, I hope you are satisfied with the result :D
– Paz
A simple question: my-Node is which element? <html>,<body>...?
– user75204
Is the body element
– Paz
No download, appears "Fault - Network error".
– user75204
@lucaswmolin this is due to a configuration in Chrome, see this link https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwj_56O7idrWAhUJQpAKHbtxDfYQFggnMAA&url=https%3A%2F%2Fproductforums.google.com%2Fd%2Ftopic%2Fchrome%2F7XBU6g6_Ktc&usg=AOvVaw3S05AkdtqqRd_aiCG-tLmS
– Paz
@lucaswmolin when succeeding using what I indicated in the reply I ask you to mark it as correct, helps other users who are having the same problem to know that you got it this way.
– Paz
Apparently it works, but with small pages, which generate images of a few KB. I am dealing with images of some MB, which give error in the download.
– user75204
@lucaswmolin in the documentation there is a mention of a compression feature to prevent this kind of error, you can also achieve this with my second code by decreasing the quality from 1 to something smaller :)
– Paz