For a project we implemented a custom button to print to current page. By using the event handlers we do further adjustment to the page, like changing the colors, displaying certain areas and all in all to make it more printable.
window.addEventListener('beforeprint', handleBeforeprint);
window.addEventListener('afterprint', handleAfterprint);
The implementation is working for only those cases, when using the print feature from the browser or by triggering the browser print by the keyboard CMD + P. Whenever I trigger the print using window.print(), the events are not triggered.