JS如何检测打印窗口的打印请求?

时间:2017-12-27 06:59:42

标签: javascript ajax forms pdf printing

我的表格和按钮名为 打印并提交

单击该按钮会调用创建PDF的Ajax请求,并使用 PrintJS 插件(http://printjs.crabbly.com/)在打印窗口中打开它

我想在打印请求(打印或取消)后调用$('#form').submit()

enter image description here

我找到了这个解决方案https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/,但它在我的情况下不起作用。 仅当我使用 CTRL + P 组合

时才有效

2 个答案:

答案 0 :(得分:0)

看起来Promise就是你要找的。

(Printjs .print()返回promise)

类似的东西:

import { Print } from 'printjs'

Print.print(...).then($('#form').submit())

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

答案 1 :(得分:0)

这是一个棘手的问题。我检查了你分享的链接,是的,它只适用于ctrl + p,也是一次(在我的情况下) 我建议尝试添加确认框并从用户那里获取输入。 您可以添加确认框后印刷方法并检查用户的输入。 根据用户输入,您可以调用submit()方法。 但它仍然是棘手的。