Print doesn't work in Chrome the first time

I have html content and image and print the page on button click. When I print it the first time, the print preview page is blank and the second time it is ok. Please help why it won't print the page the first time

The image source is in base-64 format. So, due to the large content, I cannot add the code snippet, please check with the 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();
}

      

example demo link

Someone help me with this.

+3


source to share


3 answers


win.document.write(htmlContent);
win.document.close();
win.focus();
setTimeout(function(){win.print();win.close();}, 10);

      



+3


source


Try,

change to



   var win = window.open('', '', 'toolbar=0');
    win.document.write(htmlContent);
     win.document.onload = function () {

    win.document.close();
    win.focus();
    win.print();
    win.close();
    };

      

this will work, on load make sure the page is loaded before executing the next item.

0


source


 var win = window.open('', '', 'toolbar=0');
    win.document.write(htmlContent);
    win.document.close();
    win.focus();
    setTimeout(function () { 
    win.print();},50);
    window.close();
        }   

      

this will wait 50 milliseconds before printing. So your base64 image can link to html ... (TESTED)

0


source







All Articles