javascriptsmartgwt

Print preview from multiple iframes?


I have several iframes in a page. I want to show in a print preview all the iframe contents as snapshots of iframes. I used window.print() for individual iframes, and it's working fine, but how do I do it for multiple frames?


Solution

  • You need to focus all frames one-by-one and merge in print report.

    You can achieve it, with this code:

    HTML:

    <button id="printButton" onclick="javascript:printPage()" >Print this page</button>
    
    <h1 id='header'><b>Awesome Print Report</b></h1>
    <iframe id="ifr1" src="http://amazon.com"></iframe>
    <iframe id="ifr2" src="http://amazon.com"></iframe>
    <iframe id="ifr3" src="http://amazon.com"></iframe>
    <iframe id="ifr4" src="http://amazon.com"></iframe>
    <iframe id="ifr5" src="http://amazon.com"></iframe>
    <iframe id="ifr6" src="http://amazon.com"></iframe>
    

    JavaScript:

    function printPage() {
    
        window.print();
    
        for (var k = 0; k < window.frames.length; k++) {
            window.frames[k].focus();
            window.frames[k].print();
        }
    
    }
    

    CSS:

    #header {
        margin - top: 20px;
    }
    
    @media print {
        #printButton {
            display: none;
        }
    }
    

    This CSS will hide print button on printed report.

    Here is JsFiddle for you: http://jsfiddle.net/zur4ik/r7pvF/