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

无法打印此文档,它仍在加载中-Firefox打印机错误,firefox,fonts,google-font-api,google-fonts,Firefox,Fonts,Google Font Api,Google Fonts,My API生成动态HTML文档并将其转储到弹出窗口中,如下所示: var popup = window.open('', "_blank", 'toolbar=0,location=0,menubar=1,scrollbars=1'); popup.document.write(result); 用户审阅文档后,可以打印文档 window.print(); Chrome处理时没有任何问题,但Firefox显示打印机错误: 尚无法打印此文档,它仍在加载中 仅当我按Ctrl+R时打印机

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

var popup = window.open('', "_blank", 'toolbar=0,location=0,menubar=1,scrollbars=1');
    popup.document.write(result);
用户审阅文档后,可以打印文档

window.print();
Chrome处理时没有任何问题,但Firefox显示打印机错误:

尚无法打印此文档,它仍在加载中

仅当我按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>

我觉得这和谷歌字体有关。当您将URL传递到window.open时,Firefox将加载“about:blank”,此时脚本安全性可能会阻止您通过http或https获取外部资源,我们非常感谢您提供的任何帮助

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

根据您的示例,结果是一个包含弹出窗口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");
我测试了这个结果,结果是一个字符串,包含:

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

点击“打印”链接,效果如预期

我不得不多跑一英里,但是:

我添加了服务器端代码,用于保存html文件并传递指向该文件的链接,而不是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传递的链接打开一个弹出窗口:

var popup = window.open(result.file, "_blank", 'toolbar=0,location=0,menubar=0,scrollbars=1');
最后,在模板文件中,我添加了事件侦听器,以在窗口关闭时请求删除临时文件

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

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

}, false);

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

事实上,现在我自己也尝试过了:Chrome再次起作用,Firefox弹出窗口在打开“是”后会自动关闭,我允许Firefox中出现弹出窗口