I am using this code to open and print image on new html page:
printView(dataUri:string){
var win = window.open();
win.document.write( '<img src="' + dataUri + '">');
win.print();
win.close();
}
When used like that and image is larger than few kB, print preview opens a blank page, because document is not rendered when print is invoked.
I solved that (temporarily) by introducing setTimeout
with ~200ms delay before printing, like this:
setTimeout( () => {
win.print();
win.close();
}, 200);
And this works, but I am aware that I should use some DOM/window event to wait until content is loaded. But which one?
What I tried so far:
win.document.onload = ()=>{
console.log('event fired'); // never fired
win.print();
win.close();
}
and
win.addEventListener('load', ()=>{
console.log('event fired'); // never fired
win.print();
win.close();
}
I would like to keep this Vanilla JS, so please do not refer me to jQuery window.ready.