Javascript仅打印iframe内容

use*_*382 58 javascript printing jquery

这是我的代码

<script>
var body = "dddddd"    
var script = "<script>window.print();</scr'+'ipt>";

var newWin = $("#printf")[0].contentWindow.document; 
newWin.open();
newWin.close();

$("body",newWin).append(body+script);

</script>
<iframe id="printf"></iframe>
Run Code Online (Sandbox Code Playgroud)

这可以,但它打印父页面,我如何让它只打印iframe?

mpl*_*jan 114

我不希望这样做

试着改为

window.frames["printf"].focus();
window.frames["printf"].print();
Run Code Online (Sandbox Code Playgroud)

并使用

<iframe id="printf" name="printf"></iframe>
Run Code Online (Sandbox Code Playgroud)

或者尝试好老

var newWin = window.frames["printf"];
newWin.document.write('<body onload="window.print()">dddd</body>');
newWin.document.close();
Run Code Online (Sandbox Code Playgroud)

如果jQuery不能破解它

现场演示

  • 自2014年1月22日起,无论您是添加contentWindow还是调用window.frames ["printf"].print(),这都无法在Chrome中使用.它打印整个页面,而不仅仅是框架. (5认同)
  • 我使用的是jQuery,我通过Dom对象找到了相同的方法.即`var iframe = $('#printf')[0]; iframe.contentWindow.focus(); iframe.contentWindow.print();`这在IE 9或更低版本中有效吗? (4认同)
  • @Gullbyrd我遇到了和你一样的问题,并且这段代码有效:`var PDF = document.getElementById('fileframe'); PDF.focus(); PDF.contentWindow.print();` 来自:http://www.sitepoint.com/load-pdf-iframe-call-print/ (2认同)
  • 当加载带有 pdf 数据的 iframe 时,这不起作用,例如 `&lt;iframe src="data:application/pdf;base64,JVBERi0..."&gt;&lt;/iframe&gt;`,它会给出 CORS 错误,即使框架不是甚至加载外部 URL。有谁知道如何解决这个问题? (2认同)

Sal*_*n A 17

document.getElementById("printf").contentWindow.print();
Run Code Online (Sandbox Code Playgroud)

同源政策适用.


Kia*_*ash 11

简单的方法(在ie7 +,firefox,Chrome,safari上测试)就是这样

//id is the  id of the iframe
function printFrame(id) {
            var frm = document.getElementById(id).contentWindow;
            frm.focus();// focus on contentWindow is needed on some ie versions
            frm.print();
            return false;
}
Run Code Online (Sandbox Code Playgroud)

  • 这只适用于Chrome.我很确定它会起作用! (4认同)

Eve*_*ert 9

替代选项,可能适用也可能不适合,但如果是:

如果您始终只想从页面打印iframe,则可以使用单独的"@media print {}"样式表来隐藏除iframe之外的所有内容.然后你可以正常打印页面.


Per*_*erx 7

您可以使用此命令:

document.getElementById('iframeid').contentWindow.print();
Run Code Online (Sandbox Code Playgroud)

这个命令基本上与window.print()相同,但是我们想要打印的窗口在iframe中,我们首先需要获取该窗口的实例作为javascript对象.

因此,在参考iframe时,我们首先使用它的id获取iframe,然后它的contentWindow返回一个窗口(DOM)对象.因此,我们可以直接在此对象上使用window.print()函数.


Jon*_* TJ 5

我在 IE8 中遇到了上述所有解决方案的问题,找到了一个在 IE 8+9、Chrome、Safari 和 Firefox 中测试过的不错的解决方法。对于我的情况,我需要打印动态生成的报告:

// create content of iframe
var content = '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'+
'<head><link href="/css/print.css" media="all" rel="stylesheet" type="text/css"></head>'+
'<body>(rest of body content)'+
'<script type="text/javascript">function printPage() { window.focus(); window.print();return; }</script>'+
'</body></html>';
Run Code Online (Sandbox Code Playgroud)

请注意正文结束标记之前的 printPage() javascript 方法。

接下来创建 iframe 并将其附加到父主体,以便其 contentWindow 可用:

var newIframe = document.createElement('iframe');
newIframe.width = '0';
newIframe.height = '0';
newIframe.src = 'about:blank';
document.body.appendChild(newIframe);
Run Code Online (Sandbox Code Playgroud)

接下来设置内容:

newIframe.contentWindow.contents = content;
newIframe.src = 'javascript:window["contents"]';
Run Code Online (Sandbox Code Playgroud)

在这里,我们将动态内容变量设置为 iframe 的 window 对象,然后通过 javascript: 方案调用它。

最后打印;聚焦 iframe 并在 iframe 内容中调用 javascript printPage() 函数:

newIframe.focus();
setTimeout(function() {
  newIframe.contentWindow.printPage();
}, 200);
return;
Run Code Online (Sandbox Code Playgroud)

setTimeout 不一定需要,但是如果您正在加载大量内容,我发现 Chrome 有时无法在没有它的情况下打印,因此建议执行此步骤。另一种方法是包装 'newIframe.contentWindow.printPage();' 在 try catch 中并将 setTimeout 包装的版本放在 catch 块中。

希望这对某人有所帮助,因为我花了很多时间寻找一个可以在多个浏览器中运行良好的解决方案。感谢SpareCycles

编辑:

不使用 setTimeout 调用 printPage 函数,而是使用以下内容:

newIframe.onload = function() {
    newIframe.contentWindow.printPage();
}
Run Code Online (Sandbox Code Playgroud)