Skip to content
Go back

Printing Page on Chrome Desktop and Android

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.

You can’t bypass the print dialog in Chrome without custom command line arguments, and even then, it only works on desktop. For one-click printing on mobile, you’ll need to use third-party apps. In a previous project, we managed to get silent printing working using RawBT with SUNMI devices. Our approach was to convert the div to a PNG image, then to a base64 string, and pass it to the RawBT URL scheme. However, we eventually ended up building a native app instead.


Share this post on:

Previous Post
Chrome DevTools Network Tab Filter Cheat Sheet
Next Post
Git Command Reference