Print iframe in IE
cross-browser, iframe, internet-explorer, javascript
Solution
you can try something like this:
use something like jQuery's .load() to put the report inside the `#printerDiv` and differenciate it with CSS
on your CSS you have
#printerDiv{display:none;}
@media print{
body *{display:none;}
#printerDiv{display:block;height:100%;width:100%}
}
and the print button javascript
$('#printerDiv').empty().load(url, function(){
window.print();
}); //or whatever library/pure js you like
Problem
I have a button that loads a report for print into an "invisible" iframe inside a div and prints that iframe; the user presses a button to print the report (contained on a different page) without changing pages or any visual disruption aside from the print dialog. It works in Chrome and Firefox but not IE. In IE the parent page is printed in full, and a tiny messed up iframe is inserted at the bottom of the page (where I'm loading the iframe). Here's the empty div without content, it's there so I have an ID tagged place to style and stick content with Javascript: ``` <div id="printerDiv"></div> ``` Here's the javascript function, onClick of my button this function fires and inserts my print page into an iframe inside printerDiv, after loading this page it prints: ``` function printPage(url) { var div = document.getElementById("printerDiv"); div.innerHTML = '<iframe src="'+url+'" onload=this.contentWindow.print();> </iframe>'; } ``` Here's the CSS hiding the div. I'm using absolute positioning to shift it off the visible screen area. I used to use display:none, but Firefox was unable to print iframes styled that way: ``` #printerDiv{ position:absolute; left:-9999px; } ``` When I print in IE it prints the full page, and then at the bottom, where `#printerDiv` is, I get this little iframe: So the content's being loaded, but it's not printing just the iframe, and it's not hiding the iframe properly either. Any other content I insert into `#printerDiv` is hidden properly, only the iframe displays like that. I've tried all solutions in this question in my Javascript function: using `self.prin`t, using `document.parentWindow.print`, changing the styles on the printerDiv to 0px height/width doesn't work either. I'm welcome to solutions that don't use iframes (IE seems to have massive issues with them) but I need this ability to load content not visible on screen and print it directly via a button.