I\'ve this function for my Wordpress plugin uses jQuery that prints the content from the div with class \"table_disp\". How can I print the css style along with it.
if you can add a plugin, the Jquery printThis plugin works great (since you're already using jQuery), and does exactly what you need. http://plugins.jquery.com/printThis/
It makes an iframe and uses your page css plus allows for an additional css file specifically for printing, along with other things
function PrintElem(elem) {
Popup(jQuery(elem).html());
}
function Popup(data) {
var mywindow = window.open('', 'my div', 'height=400,width=600');
mywindow.document.write('<html><head><title></title>');
mywindow.document.write('<link rel="stylesheet" href="http://www.test.com/style.css" type="text/css" />');
mywindow.document.write('<style type="text/css">.test { color:red; } </style></head><body>');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.document.close();
mywindow.print();
}
<a href="#" id="hrefPrint" onclick="PrintElem('.print_div')">Print</a>
You need to include the SAME stylesheet you're using in the parent page within the pop-up. You may want to make a dummy page stub/wrapper that has your stylesheet in it, then inject your HTML.
var myStyle = '<link rel="stylesheet" href="http://mysite.com/mystyle.css" />';
w.document.write(myStyle + jQuery('.table_disp').html());
Append Stylesheet instead
var css= '<link rel="stylesheet" href="/css/print.css" />';
var printContent = document.getElementById("printContentID");
var WinPrint = window.open('', '', 'width=900,height=650');
WinPrint.document.write(printContent.innerHTML);
WinPrint.document.head.innerHTML = css;
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();