在iFrame中将两个内容窗口打印为一个

时间:2018-11-24 09:48:56

标签: javascript html iframe printing dojo

我有一个包含多个iFrame的网页,例如6 而且我希望能够将其中两个帧打印为一个调用,而不是获取每个帧的contentWindow然后调用:

parent.iFrame1.focus();
parent.iFrame1.print();
parent.iFrame2.focus();
parent.iFrame2.print();

在浏览器的两个单独的打印预览窗口中打印内容。

我尝试了以下方法,但没有一个能真正满足我的需求或为我工作(即内容被截断了)

有可能做或者我在击败一匹死马?

我也尝试过从iframe中获取html并将其写入新文档中,但是感觉有点笨拙。另外,有多个css和脚本文件等需要合并以显示一个内聚页面。

任何答案将不胜感激!

我尝试过的尚未解决的潜在解决方案:

Wait for html to write to window before calling window.print()

Print preview from multiple iframes?

https://graciesdad.wordpress.com/2007/04/19/all-in-one-printing-from-multiple-iframes/

Javascript Print iframe contents only

1 个答案:

答案 0 :(得分:1)

我会使用另一个iframe来完成这项工作。但是,该iframe 必须位于同一域中

在您的主页上(例如mydomain / index.html)

/data/teamcity_server/datadir/lib/
└── jdbc
    └── postgresql-42.2.5.jar

connectionUrl=jdbc\:postgresql\://mydbserver.com\:5432 函数采用一组外部页面的URL进行打印。它将创建一个“不可见”的iframe(请参阅<html> <head> <style> .print { visibility: hidden; } </style> </head> <body> <script> function printIframes(urls) { var iframe = document.createElement('iframe'); iframe.className = "print"; iframe.src = `print.html?urls=${encodeURIComponent(urls.join(','))}`; iframe.onload = function () { this.contentWindow.focus(); this.contentWindow.print(); }; document.body.appendChild(iframe); } </script> </body> </html> css类),并使用查询字符串中的以下网址加载printIframes()

.iframe

如果您直接访问该iframe,则会看到以下内容:

enter image description here


在print.html 中,例如mydomain / print.html

此页面解码print.html查询字符串,并为每个网址创建一个iframe进行加载。

<iframe src="print.html?urls=http://example.com,http://example.com"></iframe>

示例

在此屏幕快照中,我加载了主页(index.html),并在开发人员控制台中调用了urls函数:(您实际上不会看到它们)

enter image description here

然后立即发出打印该iframe内容的请求。

enter image description here