2

My question, put shortly, is as follow: I would like to know if it was possible to detect the fact that a user prints a document on the internet. My goal is to display a document, allow the user to print it (with the window.print () method), and then record this event each time a user clicks on 'print'. If it is possible, could you please tell me how to do it ?

To be more precise and clear, I don't want to log the "print request", I only want to log the clic on the print button. Indeed, once the windows.print () method is launched, a preview page opens, allowing the user to print an element (1), but also to cancel the action (2) and return to the internet page where the user was trying to print an element. I only want to log the first action.

During my searches, all the answers that I found were related to the "print request" and so where only able to detect the opening of the preview page, but never precisely the click on the print button.

*As stated here in the comments of the top answer, the solution (involving window.matchMedia('print')) provided by tj vantoll are not appropriate in this context, as they only listen to the print page apparition. Also it doesn't seems to work anymore with IE and FF.

*The same goes for onbeforepint and oneafterprint which only capture the print request, as well as being unreliable because they also depends on the browser and its version.

*Another guy tried to load a server script with a background url in CSS print media. But this event is also triggered when the user opens the print preview only, without actually printing...

To conclude, I am short of ideas. Do you have a solution?

Best regards

Community
  • 1
  • 1
  • 1
    There's also [beforeprint](https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeprint) event, not sure if that helps. – Teemu Feb 16 '17 at 20:55
  • Sorry but onbeforeprint (https://www.w3schools.com/TagS/tryit.asp?filename=tryhtml5_ev_onbeforeprint) is triggered immediately when a print query is launched, before the preview page even appeared ! and for onafterprint (https://www.w3schools.com/TagS/tryit.asp?https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_onafterprint) it is triggered even when a user don't clic on the print button. And sadly these solutions only work on FF and IE... So it doesn't help :( – Guillaume_azaza Feb 17 '17 at 08:20
  • The print dialog is often the one of the OS. Even your browser doesn't know if I clicked print, cancel, save as pdf etc. So you, little web developer with no rights on my machine shall not know it either. – Kaiido Feb 20 '17 at 23:43
  • Hi Kaiido ! Thanks a lot for your clear answer. So it seems that i won't be able yo achieve what i want ... :-( – Guillaume_azaza Mar 05 '17 at 16:55

0 Answers0