You cannot capture the print command in the browser, but you can block the keyboard shortcuts (although I would not recommend it), so when the user types with ctrl/cmd + p
, he redirects to PDF (or does something else). This is a mythical usability field, but you should probably just create a large link that says "Printable Version" and link it to a PDF (or a version of a page using printable CSS).
Other good options are to simply define some rules for the print
media type in your CSS file, then browsers will apply them when the user prints without any hacks or javascript.
But since you asked that I created a small shortcut to capture the script for the print command. This is a bit complicated due to the mac command line key, but something like:
var cmd = false; $(document).on('keydown', function(e) { if(detectMacCommand(e.which)) { cmd = true; return; } // now detect print (ctr/cmd + p) if ( e.which == 80 && ( e.ctrl || cmd ) ) { e.preventDefault(); alert('redirect to PDF'); } }).on('keyup', function(e) { if(detectMacCommand(e.which)) { cmd = false; return; } }); function detectMacCommand(key) { return ( $.browser.mozilla && key == 224 || $.browser.opera && key == 17 || $.browser.webkit && ( key == 91 || key == 93 )); }
It's pretty cool, but don't use it :)
David source share