leg*_*ess 7 printing iframe internet-explorer
在我们的Web应用程序中,我们为几个页面提供了打印功能,我们采用的方法是将当前页面的内容放在全局可用的iframe文档中并打印iframe(使用Javascript).这在Firefox中完全正常,但在IE中它以非常小的字体打印iframe,几乎不可读.
在两个浏览器中应用的所有CSS都是相同的,我确保打印的HTML不会以任何方式溢出(使IE适合内容或其他东西)...而且IE打印仍然非常小.有趣的是,如果我将打印逻辑更改为写入新窗口然后执行window.print(),那么IE中的一切工作正常,并且字体与CSS要求/指定的一样大.
IE中的iframe.print()有没有遇到过类似的问题?
谢谢您的帮助.
尼廷
小智 5
我在解决同样的问题后发现了这个线程。看起来这种行为即使在 IE11 中也仍然存在。好消息是我能够找到解决方案,而无需打开新窗口然后调用window.print().
诀窍是document.execCommand在 IE 中使用(一直工作到 IE11),然后优雅地回退到iframe.print()其他浏览器中。完整的解决方案可能如下所示(使用 jQuery 选择 iframe,但这完全是可选的):
var target = $('iframe')[0];
var isPrintCommandSupported = true;
try {
isPrintCommandSupported = target.contentWindow.document.execCommand('print', false, null);
} catch(e) {
target.contentWindow.print();
}
if (!isPrintCommandSupported) target.contentWindow.print();
Run Code Online (Sandbox Code Playgroud)
这个解决方案的灵感来自于这里关于 IE7 的一个非常古老的线程: http: //bytes.com/topic/misc/answers/629926-ie7-printing-iframe-solution。不过,它仍然具有相关性。