最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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)

发布评论

评论列表(0)

  1. 暂无评论