使用setTimeout显示图像时,删除打印预览中的空白页

时间:2017-07-04 03:44:15

标签: javascript html

我在Chrome中遇到打印预览问题。我想打印里面的图像,但不显示。所以,我使用setTimeout来显示图像。图像成功显示,但问题是使用setTimeout时最后有一个空白页。

不使用setTimeout时,没有空白页面。但图片没有显示。

那么如何删除空白页?

enter image description here

我的代码:

function printDiv(divId) {
    var panel = document.getElementById(divId);
    var myWindow = window.open('', '', 'width=1200,height=790');    

    myWindow.document.write('<html><head><title>Print</title>');
    myWindow.document.write('<base href="' + location.origin + location.pathname + '">');
    myWindow.document.write('<link media="print" href="' + assets_url + 'css/lib/cetak.css" rel="stylesheet" type="text/css">');
    myWindow.document.write('</head><body>'); 
    myWindow.document.write(panel.innerHTML); 
    myWindow.document.write('</body></html>');

    myWindow.document.close();
    myWindow.moveTo(0, 0);
    myWindow.resizeTo(screen.width, screen.height);

    setTimeout(function(){
        myWindow.focus();
        myWindow.print();
        myWindow.close();       
    },100);    

    return true;
}

1 个答案:

答案 0 :(得分:0)

这是由于在document.write中使用了错误的CSS类引起的。

将CSS更改为与Bootstrap相关的适当CSS,它将起作用。

这与setTimeout函数无关,它似乎可以通过删除setTimeout来工作,但数据不会在引导程序中呈现。通过更改CSS类以更正Bootstrap CSS,它将起作用。