Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print iframe in IE

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: enter image description here

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.print, 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.

like image 374
Ben Brocka Avatar asked Oct 07 '11 13:10

Ben Brocka


2 Answers

In my case, the solution was to not hide the iframe I wanted to print. If I hide it (using display:none; visibility:hidden etc. ) the parent page would always print.

Instead of actually hiding the frame, I just made it 10x10 and borderless. Now this works (with the delay trick).

Example code:

    <script type="text/javascript">
    function printFrame(frameId, targetContent)
    {
        var doc = $('#'+frameId).get(0).contentWindow.document;
        var $body = $('body',doc);

        $body.html($('#'+targetContent).html());

        setTimeout(function(){
            $('#'+frameId).get(0).contentWindow.focus();
            $('#'+frameId).get(0).contentWindow.print();            
        }, 1000);        
    }
</script>

HTML:

<div id="contentToPrint">
I just want to print this stuff
</div>
<iframe src="about:blank" id="printframe" style="border:0" width="10" height="10"></iframe>

Button to print:

<a href="javascript:printFrame('printframe','contentToPrint')">Print frame</a>
like image 94
HaukurHaf Avatar answered Sep 21 '22 00:09

HaukurHaf


`<script>

function framePrint(whichFrame) {

parent[whichFrame].focus();

parent[whichFrame].print();

}

</script>`

<a href="javascript:framePrint('FRAMENAME');">Print</a>

Change FRAMENAME to the name of the frame that you wish to print.

like image 37
Ben Avatar answered Sep 21 '22 00:09

Ben