无法打印此文档,它仍在加载中 - Firefox 打印机错误

Cannot print this document yet, it is still being loaded - Firefox Printer Error

我的 API 生成动态 HTML 文档并将其转储到弹出窗口 window 中,如下所示:

var popup = window.open('', "_blank", 'toolbar=0,location=0,menubar=1,scrollbars=1');
    popup.document.write(result);

文档被用户审阅后,他们可以调用打印它

window.print();

Chrome 处理没有任何问题,但 Firefox 显示打印机错误:

"Cannot print this document yet, it is still being loaded"

打印机 window 只有在我按下 Ctrl+R 时才会打开。

似乎 $(document).ready() 在 firefox 中从未发生过,它一直在等待加载。

弹出窗口中的状态栏显示 Read fonts.gstatic.com

以下是文档的简要内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="https://fonts.googleapis.com/css?family=Orbitron|Jura|Prompt" rel="stylesheet"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<title>Invoice #15001</title>

<style>
...
</style>
</head>
<body>
<div id="invoice_body" >
...
</div><!-- Invoice body -->
</body>
</html>

我感觉它与 Google 字体有关。感谢任何帮助

当您将“”作为 URL 传递给 window.open 时,Firefox 会加载 'about:blank',此时脚本安全性可能会阻止您通过 http 或 https 获取外部资源。 ..

我能够重现您的问题,并在我尝试打印时弹出相同的错误——我能够通过在调用 window.open 时使用数据 url 使其正常工作...

根据您的示例,result 是一个包含弹出窗口 HTML 的字符串,因此您可以这样调用 window.open,而不再使用 document.write对于任何事情:

var popup = window.open("data:text/html;charset=utf-8,"+result, "printPopup", "toolbar=0,location=0,menubar=0,scrollbars=1");

我用 result 作为一个包含以下内容的字符串进行了测试:

<html><head>
<link rel="stylesheet"href="https://fonts.googleapis.com/css?family=Tangerine">
<style> body { font-family: 'Tangerine', serif; font-size: 48px; } </style>
<title>Test</title></head>
<body>
<div>Testing testing</div>
<div><a href="javascript:window.print()">Print</a></div>
</body>
</html>

点击打印 link 按预期工作...

我不得不加倍努力,但是:

我添加了服务器端代码,用于保存 html 文件并将 link 传递给该文件而不是 html 内容:

ob_start();
include('ezts_invoice_template.php');
$dom = ob_get_clean();
$ezts_file_path = EZTS_PLUGIN_PATH.'kernel/tmp/'.session_id().'_tmp.html';
$ezts_file = fopen($ezts_file_path, 'w+');
$result = fwrite($ezts_file, $dom);
fclose($ezts_file);
print_r('{"result":"success", "file":"'.plugin_dir_url(__FILE__).'tmp/'.session_id().'_tmp.html"}');

在 JS 中,我通过从 PHP:

传递的 link 打开一个弹出窗口
var popup = window.open(result.file, "_blank", 'toolbar=0,location=0,menubar=0,scrollbars=1');

最后,我在模板文件中添加了事件侦听器,以在 window 关闭时请求删除临时文件

window.addEventListener('beforeunload', function(event) {

    window.opener.eztsApiRequest('deleteTempFile', 
                                 '', 
                                 function(result, status){ console.log(result); });

}, false);

这并不容易,但效果很好。