简体   繁体   中英

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

I have a problem with print preview in Chrome. I want to print with images inside, but not show. So, I use setTimeout to display the image. The image successfully shows up, but the problem is when using setTimeout there is a blank page at last.

When not using setTimeout, there is no blank page. But the picture does not show.

So how do I get the blank page deleted?

在此处输入图像描述

My code:

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

This is caused by the use of incorrect CSS class in document.write.

Change the CSS to appropriate Bootstrap related CSS and it will work.

This is not related to setTimeout function, it seems to work by removing the setTimeout but the data is not rendered in bootstrap. By changing the CSS class to correct Bootstrap CSS it will work.

You can use below line to remove only URL from print view newWin = window.open("https://asdasdasd"); Sample: var upper = document.getElementById("upper");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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