My current setup involves a button that triggers the loading and printing of a report within an "invisible" iframe enclosed in a div. This process allows users to print the report from a different page without any visual disruption or changing pages, aside from the print dialog appearing. While this method works smoothly in Chrome and Firefox, it encounters issues in Internet Explorer (IE). In IE, the parent page gets printed in its entirety, and a distorted iframe is awkwardly inserted at the bottom of the page where the iframe should be loaded.
To facilitate this process, I have an empty div named printerDiv which acts as a designated location for styling and inserting content via Javascript:
<div id="printerDiv"></div>
The JavaScript function utilized here operates upon clicking the designated button, effectively inserting the desired print page into an iframe located inside printerDiv. Once the page is loaded, it initiates the printing process:
function printPage(url) {
var div = document.getElementById("printerDiv");
div.innerHTML = '<iframe src="'+url+'" onload=this.contentWindow.print();>
</iframe>';
}
In order to conceal the div during the printing process, CSS has been implemented with absolute positioning to move it off the visible screen area. Previous attempts using display:none resulted in Firefox being unable to print iframes styled in that manner:
#printerDiv{
position:absolute;
left:-9999px;
}
When attempting to print in IE, the full page is printed along with a small iframe positioned at the bottom, where #printerDiv resides. The content loads correctly, but only the iframe displays while failing to hide properly. Any other content added to #printerDiv remains hidden as intended.
I've experimented with various solutions outlined in this query within my JavaScript function: experimenting with self.print and document.parentWindow.print, adjusting the styles of printerDiv to 0px height/width, all to no avail.
If possible, I am open to alternative solutions that do not involve iframes, considering IE's notable compatibility challenges. However, the primary functionality of loading invisible content and directly initiating printing via a button press must remain intact.