Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing is not working in chrome browser at first time

I have some html contents and image and print the page on button click. When i print it first time the print preview page is empty and second time it is fine. Please help on why it is not print the page first time

Image source is base-64 format. So, due to the large content, i not able to add code snippet, Please check with demo link.

<input type="button" id="printImage" onclick=printImage() value="print" />
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();
}

sample demo link

Anyone help me on this..

like image 542
Bharathi Avatar asked Oct 20 '25 03:10

Bharathi


1 Answers

win.document.write(htmlContent);
win.document.close();
win.focus();
setTimeout(function(){win.print();win.close();}, 10);
like image 58
Saber Avatar answered Oct 22 '25 18:10

Saber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!