Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Preview Is Blank After adding External Stylesheet reference in print html content

I want to print DIV content of a page.What i do is retrieve contents of the div using JS and pass it to new window obj on which i call .print(). THe Text contents and images are displayed as such. Just when i add this line to retrieved div contents

<link href="myprintstyle.css" rel="stylesheet" type="text/css">

the print preview is blank. I tried adding other stylesheets as well,same result. Whatever style sheet reference i add to print html contents,same result -blank preview page. here is My JS code to print page contents.

var printDivCSS = new String ('<link href="myprintstyle.css" rel="stylesheet" type="text/css">');
function Popup(htmldata) 
{
    var mywindow = window.open('test', 'eChallanReceipt', 'height=800,width=800,top=20;left=20');
    var str ="<html><head><title>test</title>"; 
    str+= "</head><body>"+ printDivCSS + htmldata+"</body></html>";
     mywindow.document.write(str);
    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10

    mywindow.print();
    mywindow.close();

    return false;
}

Please suggest some fix. I want to style the print content. Browser: Chrome

The same code is working in Mozilla. But in Chrome i am facing this issue.

like image 731
zee Avatar asked Nov 08 '22 21:11

zee


1 Answers

I know this is an old question but for anyone who is having this problem now here is the solution.

It is showing blank page because the document is not finished loading yet. so to fix it add mywindow.print() in a timeout .

var printDivCSS = new String ('<link href="myprintstyle.css" rel="stylesheet" type="text/css">');
function Popup(htmldata) 
{
    var mywindow = window.open('test', 'eChallanReceipt', 'height=800,width=800,top=20;left=20');
    var str ="<html><head><title>test</title>"; 
    str+= "</head><body>"+ printDivCSS + htmldata+"</body></html>";
     mywindow.document.write(str);
    mywindow.document.close(); // necessary for IE >= 10
    $( mywindow.document).ready(function(){
  //set this timeout longer if you have many resources to load
    setTimeout(function(){
       mywindow.focus();
       mywindow.print();
    },1000);

    return false;
}
like image 99
ShegaMike Avatar answered Nov 14 '22 23:11

ShegaMike