JavaScript windows.print() 回调 - 确定打印作业何时完成 [英] JavaScript windows.print() callback - Determining when a print job completes

查看:90
本文介绍了JavaScript windows.print() 回调 - 确定打印作业何时完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:是否可以向 windows.print() 函数添加回调?我意识到没有本地解决方案,但我想知道是否有某种库可以实现这一点.注意 - 如果我需要安装某些东西,我可以访问计算机.

Question: Is it possible to add a callback to the windows.print() function? I realize there isn't a native solution, but I was wondering if there is some sort of library that accomplishes this. Note - I have access to the computers if I need to install something.

背景:我实施了一个在 Firefox (v44-46) 上运行的网络 POS 解决方案.对于某些订单,我需要在 Epson TM-T88V 打印机上打印 2 张收据.

Background: I have implemented a web POS solution that runs on Firefox (v44-46). For some orders, I need to print 2 receipts on an Epson TM-T88V printer.

问题:如果第二个 windows.print() 方法在第一张收据仍在打印时执行,Firefox 会提示某些打印功能不可用".

Problem: If the second windows.print() method executes while the first receipt is still printing, Firefox prompts "Some printing functionality is not available".

可能的解决方案:我可以增加打印调用之间的延迟.但是,这不是很可靠,因为有些订单可能很长,而其他订单则不然.此解决方案可能不适用于某些情况,而且只会减慢其他情况.

Possible Solution: I can increase the delay between print calls. However, this isn't very robust since some orders can be quite lengthy, whereas others are not. This solution might not work for some cases, and would simply slow down others.

有没有更好的方法来做到这一点?

Is there a better way to go about doing this?

推荐答案

好像你想听afterprint 事件

Seems like you want to listen for the afterprint event

window.addEventListener('afterprint', e => console.log('Ready for next print job'));

请注意,这对跨浏览器的支持较低(FireFox 6+,IE)

Please note that this has low cross-browser support (FireFox 6+, IE)

MDN 页面 还告诉我们,通过在 matchMedia('print') 上添加侦听器,可以在基于 webkit 的浏览器中实现类似的功能,因此您可以通过为这些浏览器编写如下内容来填充它

The MDN page also tells us that something similar can be achieved in webkit based browsers by adding a listener on matchMedia('print'), so you could shim it by writing something like the following for those browsers

window.matchMedia('print').addListener(mql => {
    if (!mql.matches) {
        window.dispatchEvent(new Event('afterprint'));
    }
});

这应该适用于例如谷歌浏览器

这篇关于JavaScript windows.print() 回调 - 确定打印作业何时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆