第一次在 chrome 浏览器中无法打印

Bha*_*thi 3 html javascript printing jquery

我有一些 html 内容和图像,并在单击按钮时打印页面。当我第一次打印它时,打印预览页面是空的,第二次它很好。请帮助解释为什么它不是第一次打印页面

图像源是 base-64 格式。所以,由于内容很大,我无法添加代码片段,请查看演示链接。

<input type="button" id="printImage" onclick=printImage() value="print" />
Run Code Online (Sandbox Code Playgroud)
function printImage() {
  var htmlContent = "The html code in stack-overflow exceeded. So please check with demo link for html content";
  var win = window.open();
  win.document.write(htmlContent);
  win.document.close();
  win.focus();
  win.print();
  win.close();
}
Run Code Online (Sandbox Code Playgroud)

示例演示链接

任何人都可以帮助我。。

小智 5

win.document.write(htmlContent);
win.document.close();
win.focus();
setTimeout(function(){win.print();win.close();}, 10);
Run Code Online (Sandbox Code Playgroud)