You cannot prevent the user from printing, but you can hide everything when the user prints a document using simple CSS:
<style type="text/css" media="print"> * { display: none; } </style>
Updated violin .
If you want to show the visitor a custom message when he tries to print, and not just a blank page, this is possible using client-side code, but first wrap all existing content as follows:
<div id="AllContent"> </div>
And add a container like this with a custom message:
<div class="PrintMessage">You are not authorized to print this document</div>
Now get rid of the <style type="text/css" media="print"> block and the code will be like this:
if ('matchMedia' in window) { // Chrome, Firefox, and IE 10 support mediaMatch listeners window.matchMedia('print').addListener(function(media) { if (media.matches) { beforePrint(); } else { // Fires immediately, so wait for the first mouse movement $(document).one('mouseover', afterPrint); } }); } else { // IE and Firefox fire before/after events $(window).on('beforeprint', beforePrint); $(window).on('afterprint', afterPrint); } function beforePrint() { $("#AllContent").hide(); $(".PrintMessage").show(); } function afterPrint() { $(".PrintMessage").hide(); $("#AllContent").show(); }
Code adopted from this excellent answer .
Updated violin . (shows a message when printing)
source share