I’ve a webpage with 4 charts. I’m taking separate screenshots for each of them. Then tried to put them on another canvas, show them vertically and print it as single-page pdf file. But, I’m getting an Error saying:
Uncaught TypeError: CanvasRenderingContext2D.drawImage: Argument 1 could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap.
Here is the script
JavaScript
x
55
55
1
function HTMLtoPDF() {
2
3
function verticalCanvases(cnv1, cnv2, cnv3, cnv4) {
4
var newCanvas = document.createElement('canvas'),
5
ctx = newCanvas.getContext('2d'),
6
width = cnv1.width,
7
height = cnv1.height + cnv2.height + cnv3.height + cnv4.height;
8
9
newCanvas.width = width;
10
newCanvas.height = height;
11
12
[{
13
cnv: cnv1,
14
y: 0
15
},
16
{
17
cnv: cnv2,
18
y: cnv1.height
19
},
20
{
21
cnv: cnv3,
22
y: cnv1.height + cnv2.height
23
},
24
{
25
cnv: cnv4,
26
y: cnv1.height + cnv2.height + cnv3.height
27
28
}].forEach(function(n) {
29
ctx.beginPath();
30
ctx.drawImage(n.cnv, 0, n.y, width, n.cnv.height);
31
});
32
33
var imgdata = newCanvas.toDataURL();
34
35
return imgdata;
36
}
37
38
var forms = $('[id^=caspioform]');
39
40
var canvas1 = html2canvas(forms[3]);
41
var canvas2 = html2canvas(forms[5]);
42
var canvas3 = html2canvas(forms[7]);
43
var canvas4 = html2canvas(forms[9]);
44
45
var dURL = verticalCanvases(canvas1, canvas2, canvas3, canvas4);
46
47
var doc = new jsPDF("p", "mm", "a4");
48
49
var width = doc.internal.pageSize.getWidth();
50
var height = doc.internal.pageSize.getHeight();
51
52
doc.addImage(dURL, 'PNG', 0, 0, width, height);
53
54
doc.save('sample.pdf');
55
}
Advertisement
Answer
Since you didn’t mention it, I’ll assume html2canvas
is coming from https://github.com/niklasvh/html2canvas
In that case, the issue here is that hmtl2canvas
returns a Promise
and that’s what you’re passing to verticalCanvases
instead of the actual canvas element.
To fix it just transform the function in an asynchronous one so you can use async
/await
:
JavaScript
1
59
59
1
// |
2
// |
3
// v
4
async function HTMLtoPDF() {
5
6
function verticalCanvases(cnv1, cnv2, cnv3, cnv4) {
7
var newCanvas = document.createElement('canvas'),
8
ctx = newCanvas.getContext('2d'),
9
width = cnv1.width,
10
height = cnv1.height + cnv2.height + cnv3.height + cnv4.height;
11
12
newCanvas.width = width;
13
newCanvas.height = height;
14
15
[{
16
cnv: cnv1,
17
y: 0
18
},
19
{
20
cnv: cnv2,
21
y: cnv1.height
22
},
23
{
24
cnv: cnv3,
25
y: cnv1.height + cnv2.height
26
},
27
{
28
cnv: cnv4,
29
y: cnv1.height + cnv2.height + cnv3.height
30
31
}].forEach(function(n) {
32
ctx.beginPath();
33
ctx.drawImage(n.cnv, 0, n.y, width, n.cnv.height);
34
});
35
36
var imgdata = newCanvas.toDataURL();
37
38
return imgdata;
39
}
40
41
var forms = $('[id^=caspioform]');
42
43
var canvas1 = await html2canvas(forms[3]); // <--
44
var canvas2 = await html2canvas(forms[5]); // <--
45
var canvas3 = await html2canvas(forms[7]); // <--
46
var canvas4 = await html2canvas(forms[9]); // <--
47
48
var dURL = verticalCanvases(canvas1, canvas2, canvas3, canvas4);
49
50
var doc = new jsPDF("p", "mm", "a4");
51
52
var width = doc.internal.pageSize.getWidth();
53
var height = doc.internal.pageSize.getHeight();
54
55
doc.addImage(dURL, 'PNG', 0, 0, width, height);
56
57
doc.save('sample.pdf');
58
}
59