yWorks / svg2pdf.js

A javascript-only SVG to PDF conversion utility that runs in the browser. Brought to you by yWorks - the diagramming experts
MIT License
655 stars 101 forks source link

Images may not be ready yet when drawn to the canvas #26

Closed fskpf closed 3 years ago

fskpf commented 7 years ago

During image conversion, we assign the image.src and immediately draw it to a canvas:

var imageUrl = node.getAttribute("xlink:href") || node.getAttribute("href");
var image = new Image();
image.src = imageUrl;
...
context.drawImage(image, 0, 0, width, height);

The assignment is asynchronous and we should wait for image.onload to fire before drawing it into the canvas.

Possible workaround: Preload images before even starting the conversion. See attached file which outlines the approach (changes are marked by //***). Due to the change, the main function returns a Promise then. svg2pdf_js.txt

Rather than preloading the images we should actually use image.onload where the image is converted. However, due to the asynchronous assignment, this would need additional changes in the other parts of the code.

HackbrettXXX commented 3 years ago

Should be fixed in 2.0