Currently, I am implementing code obtained from an answer on Stack Overflow to enable direct printing of a separate page upon clicking a print button. The mechanism involves sending a URL to the print function which then loads the separate page into an iframe with a display:none
attribute and prints it onLoad
. While this method works seamlessly in Internet Explorer and Chrome, Firefox does not respond as intended. It appears that Firefox refuses to print an iframe that is set to display:none
, as shown here:
<div id="printerDiv" style="display:none"></div>
Is this behavior expected? Despite trying various CSS techniques to conceal the div containing the iframe, it remains visible in some form. Currently, I am using the following CSS to render the iframe invisible:
#printerDiv iframe{
width:1px !important;
height:1px !important;
border:0 !important;
margin:0 !important;
}
However, even with these adjustments, there is still a noticeable 14px gap once the iframe is generated.
Is there a way to completely hide the iframe without utilizing the display:none
attribute? Ideally, is there a cleaner alternative to achieve this functionality?
I have also experimented with setting the iframe to display:block
for @print media types and display:none
for screen, but the JavaScript function fails to initiate the printing process.