3
<script type="text/javascript">
function printDoc() {
   document.getElementById("frame_singleCheque").contentWindow.print();
}
</script>

<iframe  style ="height:400px; width: 750px; overflow:scroll;"  id="frame_singleCheque" src="http://www.w3schools.com"></iframe>
<input type="button" id = "btnCPrint" value = "Print" onclick="javascript:printDoc()" />

Error

[16:41:44.054] Error: Permission denied to access property 'print' @ http://localhost/pdf/print.php:3

i have verified with lot of stack suggested threads for print iframe content. but for me those are not worked.

Exactly above code only present in my print.php file. i want to print the iframe content.

Also i want to know, how to print the specific div which is present inside the iframe. example in this iframe " class = 'example_code notranslate' " .

Bharanikumar
  • 25,457
  • 50
  • 131
  • 201
  • First `focus()` the `window` in `iframe` and then simply `print()`. – Teemu Aug 28 '13 at 11:20
  • 1
    do you have access to the contents of iframe? is it your page? – Reigel Gallarde Aug 28 '13 at 11:21
  • 1
    you cannot inject/get content into/from a page from a different domain it violates the security sandbox. – Patrick Evans Aug 28 '13 at 11:23
  • Since HTML5, you get more control over iFrames. By using the `sandbox` or `seamless` attributes, you can integrate the iframe even further in your webpage - http://www.w3ctutorial.com/html5-tags/tag-iframe – BlueCacti Nov 24 '14 at 22:35

3 Answers3

4

You can print a cross-domain iframe page perfectly by nesting the cross domain iframe in a locally hosted iframe. A "proxy iframe".

This way the parent javascript can print the proxy iframe without issue since it's local, which will transitively print it's inner iframe originating from another domain.

This technique works and has been verified by myself.

In your container page, host the iframe like this

 <iframe id="printf" name="printf" class="A4" src="/SomeController/IFrameProxy?url=TARGETURL"></iframe>

The proxy iframe should look like something like this

        @model string
        <html>
            <head>
                <title>IFrame Proxy</title>
                <style>
                    html, body, iframe {
                        height: 100%;
                        width: 100%;
                        margin: 0;
                        padding: 0;
                        border-style: none;
                    }
                </style>

            </head>
            <body>
                <iframe src="@Model" seamless></iframe>
            </body>
        </html>

The javascript that prints the iframe (defined on container page) should look something like this:

        function printFrame() {
            var frm = document.getElementById("printf").contentWindow;
            frm.focus();// focus on contentWindow is needed on some ie versions
            frm.print();
        }
Herman Schoenfeld
  • 8,464
  • 4
  • 38
  • 49
  • seamless is probably what was allowing this to work. It is not supported by any browsers today. http://caniuse.com/#feat=iframe-seamless – dgrant069 Aug 23 '16 at 21:13
-1

It look like you've got a pretty standard cross-domain iframe issue here - namely that browsers are designed so you can't do this. There's a lot of fudges I could suggest, but largely, iframes are the wrong solution to this problem.

Basically, your best bet is to scrape the page - make a http request and parse out the content you want. Then you can interact with it as part of your own page's DOM.

user2722002
  • 65
  • 1
  • 6
-1

How do I print an IFrame from javascript in Safari/Chrome
Print iFrame Content (#5)
Print contents of IFRAME from parent window

Community
  • 1
  • 1
BlueCacti
  • 9,729
  • 3
  • 20
  • 24