Print iframe in IE

亡梦爱人 提交于 2019-12-04 21:31:32

问题


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


回答1:


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



回答2:


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>



回答3:


`<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.




回答4:


I was having the same issue recently with IE10, added a timeout and got the result I wanted, the iFrame printed out instead of the parent page:

<script type="text/javascript">
function selfPrint(){
    self.focus();
    self.print();
}
setTimeout('selfPrint()',2000);
</script>



回答5:


I had a the same problem with IE11. It seemed like IE was ignoring the .focus() call and instead printing the parent page as well as the iframe. My solution was to inject a style tag that hid everything except the iframe right before printing then removing the style after the call to print. My code is here http://ironlasso.com/print-only-lightbox-or-modal-window-content/



来源:https://stackoverflow.com/questions/7687936/print-iframe-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!