如何打印iframe内的特定内容

2024-03-26

<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

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

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

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


通过将跨域iframe嵌套在本地托管的iframe中,可以完美打印跨域iframe页面。 “代理 iframe”。

这样父 javascript 就可以毫无问题地打印代理 iframe,因为它是本地的,这将间接打印源自另一个域的内部 iframe。

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

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

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

代理 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>

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

        function printFrame() {
            var frm = document.getElementById("printf").contentWindow;
            frm.focus();// focus on contentWindow is needed on some ie versions
            frm.print();
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何打印iframe内的特定内容 的相关文章

随机推荐