在 chrome 中从 javascript 打印:如何知道打印完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8227746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:43:30  来源:igfitidea点击:

Printing from javascript in chrome: how to know printing is done?

javascriptgoogle-chromeprinting

提问by Pl4n3

In my chrome browser version 17.0.942.0 winvista the printdlg is not the usual modal system-printdlg but renderered within the websiteitself. So I want to print canvas content as described here. But the code

在我的 chrome 浏览器版本 17.0.942.0 winvista 中,printdlg 不是通常的模态 system-printdlg,而是在网站本身内呈现的。所以我想打印这里描述的画布内容。但是代码

window.print(); window.close();

window.print(); window.close();

wont work, because window.closealso closes the printdlg. So in chrome window.closemust be initiated by the user or delayed somehow. Is there a way to know if window.printwas finished, so that the window can be closed automatically?

不会工作,因为window.close也会关闭printdlg。所以在 chrome 中window.close必须由用户启动或以某种方式延迟。有没有办法知道是否window.print完成,以便窗口可以自动关闭?

采纳答案by TehWan

Check this comment on a bug report similar to what you are experiencing: http://code.google.com/p/chromium/issues/detail?id=92107#c31

在类似于您遇到的错误报告中查看此评论:http: //code.google.com/p/chromium/issues/detail?id=92107#c31

It seems like window.printis async, the workaround is to check for a mouse movement event after the print, which would occur when the print dialog is closed, then you can perform the rest of your actions.

似乎window.print是异步的,解决方法是在打印后检查鼠标移动事件,这会在打印对话框关闭时发生,然后您可以执行其余操作。

Also see: Chrome window.print() window.close() results in 'print preview failed'. Solution?

另请参阅:Chrome window.print() window.close() 导致“打印预览失败”。解决方案?

回答by Peru

add this script to your popup print window

将此脚本添加到您的弹出打印窗口

<script src="/assets/print_window_close.js" type="text/javascript"></script>

contents of print_window_close.js is

print_window_close.js 的内容是

jQuery(document).ready(function() {

    setTimeout(function() {
        window.close();
    }, 1);

});

basically seTimeout is called only when the document is focused which happens only when the overlay print dialog is closed. hence this works exactly the same way you wanted.

基本上 seTimeout 仅在文档​​被聚焦时调用,这仅在覆盖打印对话框关闭时发生。因此,这与您想要的方式完全相同。

回答by arioch1984

You could try to use window.onafterprint. For example:

您可以尝试使用 window.onafterprint。例如:

window.onafterprint = function(){console.log('Print finished...')}

Then you could find more details in this post: http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

然后你可以在这篇文章中找到更多细节:http: //tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

回答by Vagner do Carmo

That works for me, you have to make listeners to print event: https://gist.github.com/vluzrmos/085975de3e1840936604

这对我有用,你必须让听众打印事件:https: //gist.github.com/vluzrmos/085975de3e1840936604