Your code is too complicated.
Try this instead
document.getElementById("printVoucher").addEventListener("click",function(e) {
const img = e.target.dataset.src;
const html = `<body onload="window.print();setTimout(function() {window.close()},500)"><img src="${img}" /></body>`;
const pwa = window.open("", e.target.target);
if (pwa) {
pwa.document.open();
pwa.document.write(html);
pwa.document.close();
e.preventDefault(); // cancel the link
}
else e.target.href=img; // popup blocker. Open the image in a new tab
})
<a href="#" target="_blank" id="printVoucher" data-src="output/image.jpg">Print Voucher</a>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…