How to apply css to an open page with window.open?

1

I have a page that I need to print only one part of, I used window.open followed by a window.print for this, except that the output does not come out in the expected format, it's a form and it should be printed as it is in a css file I have, how do I when to create this new window it contain the css? I tried with href, target_blank and referencing in document.write but there was no way.

follows the code used to open the print page:

$('#btnPrint').click(function() {
                var prtContent = document.getElementById("dados");
                var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
                WinPrint.document.write(prtContent.innerHTML,);
                WinPrint.document.close();
                WinPrint.focus();
                WinPrint.print();
                document.getElementById('dados').remove()
                WinPrint.close();
            });
    
asked by anonymous 04.07.2018 / 21:32

1 answer

0

You can rebuild another html in this print window and call the CSS that way, for example

WinPrint.document.write('<html><head><title>Print it!</title><link rel="stylesheet" type="text/css" href="styles.css"></head><body>');
WinPrint.document.write($("#content").html());
WinPrint.document.write('</body></html>');

Here you can see other ways in this STOF response in English: link

    
04.07.2018 / 21:54