code: function attachOnBeforePrintEvent($iframe, beforePrintHandler) {
                  var win = $iframe.get(0);
                  win = win.contentWindow || win.contentDocument || win;
  
                  if (typeof beforePrintHandler === "function") {
                      if ('matchMedia' in win) {
                          win.matchMedia('print').addListener(function(mql) {
                              if(mql.matches)  beforePrintHandler();
                          });
                      } else {
                          win.onbeforeprint = beforePrintHandler;
                      }
                  }
              }
docs: attach event handler function to beforePrint event