JS, How to detect the print requests of the print window?

青春壹個敷衍的年華 提交于 2021-02-19 07:33:11

问题


I have a form and button in it called Print and Submit

The click on that button calls Ajax request which is creating PDF and open it in Print Window using PrintJS plugin ( http://printjs.crabbly.com/ )

I want to call $('#form').submit() after print requests ( Print or Cancel )

I've found this solution https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/ but it doesn't work in my case. It works only when I'm using CTRL + P combination


回答1:


Looks like Promise is what you are looking for.

(Printjs .print() returns promise)

Something like:

import { Print } from 'printjs'

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

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




回答2:


This is a tricky problem. I checked the link you shared and yes it works with ctrl+p only and that too once (in my case) I would suggest to try and add confirm box and take input from user. You can add confirm box afterprint method and check on the input from user. Based on user input you can call the submit() method. But still it is tricky one.



来源:https://stackoverflow.com/questions/47987759/js-how-to-detect-the-print-requests-of-the-print-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!