javascript - printing is not working in chrome browser at first time - Stack Overflow

I have some html contents and image and print the page on button click. When i print it first time the

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..

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..

Share Improve this question edited Apr 19, 2017 at 10:17 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Apr 19, 2017 at 10:04 BharathiBharathi 1,3382 gold badges14 silver badges45 bronze badges
Add a ment  | 

3 Answers 3

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

Try it out,

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 will make sure that the page is loaded before executing next item.

 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 for 50 miliseconds before print execute .so your base64 image can bind on html...(TESTED)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745442358a4627886.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信