Skip to content

Printing Page on Chrome Desktop and Android

Posted on:March 20, 2023 at 12:58 PM

If you need to print a document on Chrome Desktop or Android, the window.print API provides a simple solution. When you invoke window.print, the browser will open the print dialog, allowing the user to choose a printer and print the document. However, if you need to perform some actions before and after the print dialog is shown and closed, you can use the following events:

window.addEventListener("beforeprint", beforePrint);
window.addEventListener("afterprint", afterPrint);

The beforeprint event is fired when the print dialog is opened, and the afterprint event is fired when the user closes the print dialog, either by printing or cancelling. These events work well on Chrome Desktop, but on Chrome for Android, the afterprint event is fired immediately, even if the user has not yet printed or cancelled. This is a known bug in Chrome for Android, and a workaround has been suggested in the comments of this issue.