javascript 如何在 iframe 中打印特定内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18486363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:07:26  来源:igfitidea点击:

How do print specific content inside the iframe

javascriptjqueryiframe

提问by Bharanikumar

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

我已经验证了许多用于打印 iframe 内容的堆栈建议线程。但对我来说这些都不起作用。

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

上面的代码只存在于我的 print.php 文件中。我想打印 iframe 内容。

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

我也想知道,如何打印 iframe 中存在的特定 div。此 iframe 中的示例“class = 'example_code '”。

采纳答案by Herman Schoenfeld

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

通过将跨域 iframe 嵌套在本地托管的 iframe 中,您可以完美地打印跨域 iframe 页面。“代理 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.

通过这种方式,父 javascript 可以毫无问题地打印代理 iframe,因为它是本地的,这将传递地打印来自另一个域的内部 iframe。

This technique works and has been verified by myself.

这个技术是有效的,并且已经被我自己验证过。

In your container page, host the iframe like this

在您的容器页面中,像这样托管 iframe

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

The proxy iframe should look like something like this

代理 iframe 应该看起来像这样

        @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:

打印 iframe(在容器页面上定义)的 javascript 应如下所示:

        function printFrame() {
            var frm = document.getElementById("printf").contentWindow;
            frm.focus();// focus on contentWindow is needed on some ie versions
            frm.print();
        }

回答by user2722002

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.

看起来您在这里遇到了一个非常标准的跨域 iframe 问题 - 即浏览器的设计使您无法执行此操作。我可以建议很多软糖,但在很大程度上,iframe 是解决这个问题的错误方法。

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.

基本上,最好的办法是抓取页面 - 发出 http 请求并解析出您想要的内容。然后,您可以将其作为您自己页面的 DOM 的一部分与之交互。