How can i convert an HTML to a JPG/PNG image? I already tried various options but none works for me, they only make the image of some parts of the HTML.
Example with html2canvas:
TS
JavaScript
x
6
1
capturar() {
2
html2canvas(document.body).then(function(canvas) {
3
document.body.appendChild(canvas);
4
});
5
}
6
HTML
JavaScript
1
16
16
1
<div id="capture">
2
<ion-grid class="grid-padding-info-verde">
3
<ion-row class="row-info">
4
5
<ion-col size="12">
6
<ion-icon name="checkmark-circle" class="check-icono"></ion-icon>
7
</ion-col>
8
9
<ion-col size="12" class="info-col">
10
<span class="trans-sub"> {{ this.fecha }}</span>
11
</ion-col>
12
13
</ion-row>
14
</ion-grid>
15
</div>
16
This error I get when I use the example:
“DOMException: Failed to set the ‘adoptedStyleSheets’ property on ‘ShadowRoot’: Sharing constructed stylesheets in multiple documents is not allowed”
Do you know any other tools? Basically I need to take a screenshot of only a part of my screen not the full screen. Any ideas?
Advertisement
Answer
You can use the package: https://www.npmjs.com/package/dom-to-image
In your page:
JavaScript
1
11
11
1
import domtoimage from 'dom-to-image';
2
3
capturar(){
4
var node = document.getElementById('capture');
5
var options = {quality: 1};
6
7
domtoimage.toJpeg(node, options).then((dataUrl) => {
8
console.log(dataUrl) //Image in base64 jpeg
9
});
10
}
11