A possible cause of the rendering problem may be due to the relative paths that you use for the CSS file and images. First try using absolute paths. The fact that the HTML structure is rendered eliminates the problem of creating a new HTML document. Also, add media="print" to the link element.
This code works, although partially:
(function() { function createPopup( data ) { var mywindow = window.open( "", "new div", "height=400,width=600" ); mywindow.document.write( "<html><head><title></title>" ); mywindow.document.write( "<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\"/>" ); mywindow.document.write( "</head><body >" ); mywindow.document.write( data ); mywindow.document.write( "</body></html>" ); mywindow.print(); //mywindow.close(); return true; } document.addEventListener( "DOMContentLoaded", function() { document.getElementById( "print" ).addEventListener( "click", function() { createPopup( document.getElementById( "content" ).innerHTML ); }, false ); }); })();
I removed the .close () call. Keep in mind that some CSS styles may not work with printing.
source share