繁体   English   中英

去除使用setTimeout显示图片时打印预览中的空白页

[英]Remove the blank page in the print preview when using setTimeout to display the image

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

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

那么如何删除空白页呢?

在此处输入图像描述

我的代码:

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;
}

这是由于在 document.write 中使用了不正确的 CSS 类造成的。

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

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

您可以使用下面的行从打印视图中仅删除 URL newWin = window.open("https://asdasdasd"); 示例: var upper = document.getElementById("upper");

            newWin = window.open("https://asdasdasd");
            newWin.document.write(upper.outerHTML);
            setTimeout(function () {
             
                newWin.print();
                newWin.close();
            }, 400);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM