如何在iframe中打印特定内容 [英] How do print specific content inside the iframe

查看:81
本文介绍了如何在iframe中打印特定内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < script type =text / javascript> 
函数printDoc(){
document.getElementById(frame_singleCheque)。contentWindow.print();
}
< / script>

< iframe style =height:400px; width:750px; overflow:scroll; id =frame_singleChequesrc =http://www.w3schools.com>< / iframe>
< input type =buttonid =btnCPrintvalue =Printonclick =javascript:printDoc()/>

错误

  [16:41:44.054]错误:拒绝访问属性'print'的权限@ http://localhost/pdf/print.php:3 

我已经验证了很多用于打印iframe内容的堆栈建议线程。但对我来说,那些没有用。



恰好高于我的print.php文件中的代码。我想打印iframe内容。



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

解决方案

您可以通过将跨域iframe嵌套在本地托管的iframe中来完美打印跨域iframe页面。一个代理iframe。



这样,父javascript可以打印代理iframe而不会出现问题,因为它是本地的,它将传递打印来自另一个域的内部iframe。 / p>

此技术有效并且已由我自己验证。



在您的容器页面中,托管iframe,如下所示

 < iframe id =printfname =printfclass =A4src =/ SomeController / IFrameProxy?url = TargetURL中>< / iframe中> 

代理iframe看起来应该是这样的

  @model string 
< html>
< head>
< title> IFrame代理< / title>
< style>
html,body,iframe {
身高:100%;
宽度:100%;
保证金:0;
填充:0;
border-style:none;
}
< / style>

< / head>
< body>
< iframe src =@ Modelseamless>< / iframe>
< / body>
< / html>

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

  function printFrame(){
var frm = document.getElementById(printf)。contentWindow;
frm.focus(); //在某些版本上需要关注contentWindow,即版本
frm.print();
}


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

解决方案

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();
        }

这篇关于如何在iframe中打印特定内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆