Skip to content
Advertisement

How can I combine multiple images into one page and print it as a pdf?

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

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement