0

I created a JavaScript function to print the content of my iFrame.

    <a href="#" onclick="printBGReport();" align="center">Print Report</a> <br/>
    <script>
        function printBGReport(){
            var content = document.getElementById("printBGReport").innerHTML;
            var mywindow = window.open('', 'Print', 'height=600,width=800');
            mywindow.document.write(content);
            mywindow.document.close();
            mywindow.focus()
            mywindow.print();
            mywindow.close();
            //return;
        }
    </script>
<div id="printBGReport">
    <iframe id="reportBGID" name="reportBGID" src="myiFrameURL" width="900" height="1000" align="center" target="_self" valign="top" scrolling="yes" frameborder="no">
</div>

However, it is not printing all the content. Looks like it is just print the first page. Does anyone know why?

Thanks

3
  • How are the iframe contents structured exactly? What do you mean by "first page"? Commented Dec 18, 2018 at 17:08
  • The iframe content is structured in my <iframe> tag. It is a html content from another website. "first page" because it is a big content (multiples pages when print), but only the first page is printing.
    – Code Guy
    Commented Dec 18, 2018 at 17:10
  • It is just printing until the iframe height. height="1000"
    – Code Guy
    Commented Dec 18, 2018 at 17:13

1 Answer 1

6

Assuming the iframe is pointing to a page on the same domain (see CORS restrictions), you can call iframe.contentWindow.print().

document.getElementById("reportBGID").contentWindow.print()

If the iframe is pointing to another domain (which you have control of), you could use postMessage to trigger a function on the page to call window.print().


Alternatively, you could copy the iframe into a new window, and print that:

function printIframe(iframe) {
  const tempWindow = window.open('', 'Print', 'height=600,width=800')

  const newIframe = document.createElement('iframe')
  newIframe.src = iframe.src

  newIframe.style = `border: 0; width: 100%; height: 100%;`
  tempWindow.document.body.style = `margin: 0;`

  tempWindow.document.body.appendChild(newIframe)

  newIframe.onload = () => {
    tempWindow.print()
    tempWindow.close()
  }
}
<button onclick="printIframe(document.getElementById('iframe'));">
    Print Iframe
    </button>

<iframe id="iframe" src="https://example.com"></iframe>

10
  • It is not in the same domain and I don't have control of the domain
    – Code Guy
    Commented Dec 18, 2018 at 17:12
  • @CodeGuy Then it's impossible due to CORS restrictions
    – Sam Denty
    Commented Dec 18, 2018 at 17:14
  • It is just printing until the iframe height. height="1000"
    – Code Guy
    Commented Dec 18, 2018 at 17:17
  • @CodeGuy Iframes don't have innerHTML, because they are shadow elements. The only way to get the iframe's content is to use contentWindow
    – Sam Denty
    Commented Dec 18, 2018 at 17:18
  • That's why I used a div. Then, I put the iframe inside the div and I print the div.
    – Code Guy
    Commented Dec 18, 2018 at 17:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.