17

How can we stop users to print webpage using different methods?

  1. Disabling Right Click
  2. Disabling CtrlP combination of keys
  3. Right click -> print

Is there any way to stop printing webpages?

Can we handle these 3 events using javascript. Or we can say . if user will do any of these events. then i want to run my other code .Is this possible?

Gourav khanna
  • 1,381
  • 1
  • 11
  • 15

11 Answers11

28

As has already been noted, you cannot do this. However, one commonly used trick is to try and hide all the page content if it's being printed:

<style type="text/css" media="print">
    body { visibility: hidden; display: none }
</style>

But this is not guaranteed to work, and it's easy to get around if you have even a vague idea what you're doing.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • This works only until one is trying to print using the browser's print feature. A tool like Fireshot can capture the page as an image and can be printed without any problem – Kangkan Jul 15 '11 at 17:12
  • @Kangkan - Very true. As mentioned in various other answers there is *no solution* to this problem. The method used in my answer will only serve to put off someone who doesn't really need to print the page. If you a desperate to print it, there is always going to be a way. – James Allardice Jul 15 '11 at 17:15
  • I feel the question is drifting away from being a relevant question. The question is just a desperate attempt to stop printing. The rationale for sucha a desperation should come out now. – Kangkan Jul 15 '11 at 17:20
22

A webpage on a user's machine is out of your control. A user can do whatever he or she wants to do, however you can serve a PDF and disable printing that PDF. Still, if I have to print some thing I will find a way to print it.

Kumar
  • 5,038
  • 7
  • 39
  • 51
  • Even with a PDF no-print flag, that only works for compliant PDF readers. A sophisticated-enough user will have no trouble printing it anyway. – deceze Jul 11 '11 at 08:14
  • +1 yeeeah I like the Mad Max part '...user's machine is out of your control.' – Roko C. Buljan Jul 11 '11 at 08:33
  • 1
    It's about keeping honest users honest. In my case, we're asking our users not to print. They might forget, or miss the message. So, preventing a "normal" attempt to print is all we need. They're not expected to go out of the way to circumvent the basic level of prevention. – Dustin Graham Aug 14 '14 at 23:13
  • 1
    I've noticed that whenever a user ask on SO how to prevent people from copying their content (block copy/paste, block printing) there will be answer stating that you can always find your way to disable systems to block copy. But 99% of website users (and 100% of mine) are not tech savy enough to go into the dev tools to disable a style tag that prevents printing your page. You have to undestand that blocking MOST users for a paywall website hosting expensive to made content is extremely important for some people (like me), Otherwise my users share to their friends and we are RUINED. – Ilan Schemoul Apr 19 '20 at 20:32
  • Please, check James Allardice's answer (linked below)... true, the Developer Tools still allow you to print it, but not everyone is capable of doing that. https://stackoverflow.com/questions/6647392/how-to-stop-user-from-printing-webpages-using-javascript-or-jquery/6647432#6647432 – xCovelus Oct 17 '21 at 14:52
14

I know this question was asked and answered a long time ago, but I came across it and felt that I should throw my 2 cents in. I have a program that has some copyright information in it, we would prefer that the end user not be able to print this information, so what I did was create a separate div that I gave the class .printable.

I added these lines to the CSS sheet

.printable { display:none; }
@media only print {
    .container { display:none !important; } <-- This is the wrapper container for all of the site data
    .printable { display:block !important; } <-- This is my added printable container with a message about not printing the page
}

With the improvements in the new CSS engines in the major browsers, even if they do a right click + print, it will display the .printable box instead of the content.

However, it has been pointed out, that there is no way to prevent a user from using a piece of screen capture software to print the information if they really wanted to. We felt that by discouraging the printing, we will stop about 99% of the people who might have otherwise printed the information.

So that's my 2 cents... hope it helps someone

Chris
  • 439
  • 7
  • 16
11

You can't. Stop wasting your time.

As soon as the user has downloaded the data of your website to his computer, you have no control over it anymore. If you don't want the user to do with it what he likes, don't put it on the public web.

deceze
  • 510,633
  • 85
  • 743
  • 889
5

Add such block to your CSS

@media print {
  body {
    display: none;
  }
}

This should hide all content when printing.

Gedrox
  • 3,592
  • 1
  • 21
  • 29
  • 1
    This works only until one is trying to print using the browser's print feature. A tool like Fireshot can capture the page as an image and can be printed without any problem. – Kangkan Jul 15 '11 at 17:11
  • Of course you can't secure such cases. User can also capture the screen using camera you know. – Gedrox Jul 17 '11 at 19:25
  • this can be changed from browser inspection – Sumon Bappi Jun 27 '21 at 10:07
2

I tried this way of Chris

@media only print {
.container { display:none !important; }
}

it's work. But when I press F12 to open developer tool, find the line ".container { display:none !important; }" delete it, and then ctrl + p, every thing are show again on print windows. May be have no solution with 100% for prevent print webpage content.

LuyenPham
  • 21
  • 3
2

It is not possible to stop web user to print your webpage. But yes you can set what to print after processed for print. My mean simply write code like below:

<!DOCTYPE html>
<html>
    <body onbeforeprint="abortPrint()">
        <p>This is my Content</p>

        <script>    
            function abortPrint()
            {
                alert("Printing is not allowed");
                document.write();
            }
        </script>
        
    </body>
</html>

This will simply print only blank page and your content will be not printed. But this only the way with the help of this you can stop user to print page.

Suggestion to answer is always appreciated.

  • Tested on firefox, and didn't work can you please check on your side ? I had to change it to window.onbeforeprint = () => document.write("print is nt allowed"); – phoenixstudio Jan 02 '21 at 11:10
  • @phoenixstudio Yes, you are right. My answer is applicable to only body of html but your answer is covering all window. – Nayankumar Korat Jun 24 '21 at 13:38
2

There's just no reliable way to do this. You can intercept certain key presses etc and cancel them using script but when the user has script disabled then there's no prevention. Also, the print functionality is built into the actual browser itself, you can't actually prevent this.

Even browser plugins such as Aviary will grab a screenshot of the browser and copy it into memory so you wouldn't be able to prevent this happening. The user could then just print from photoshop or paint .net etc.

The only thing I would suggest you try is that you can include a print.css only stylesheet. Within that stylesheet you may wish to try setting any sensitive content as display:none. However, even this is not guaranteed as the user could simply disable stylesheets.

Brian Scott
  • 9,221
  • 6
  • 47
  • 68
2

I wouldn't fight the users expectations. Don't change the default behaviour.

Dimitar Petrov
  • 667
  • 1
  • 5
  • 16
  • Can we handle these 3 events using javascript. Or we can say . if user will do any of these events. then i want to run my other code .Is this possible? – Gourav khanna Jul 11 '11 at 08:20
0

If you render something onto the browser of the user, it becomes in the control of the user. You can not resist the user from printing.

If you wish some information to be shown to the user, and you do not want the user to be able to print, you can use something like a PDF and securing it against printing.

What is your kind of usage?

Kangkan
  • 15,267
  • 10
  • 70
  • 113
0

Yes you can do it with JavaScript This script will disable everything from which a user can print like from the console or right clicking or ctrl+p And use it with css @media print{body{display:none;}} For a extra security.

Disable context menu on right-click,

$("body").on("contextmenu", function (e)  
   {  
      return false;  
   });  
}); 
//Or,
document.oncontextmenu = function() {
   return false;
}
//Disable right-click menu on a particular section on the page,
$(document).ready(function(){  
   $("#youDivSectionId").bind("contextmenu", function(e) {  
      return false;  
   });  
}); 
//Disable Cut, copy, paste,
$(document).ready(function(){
   $('body').bind('cut copy paste', function (e)
   {
      e.preventDefault();
   });
});
//Let's Block the same cut, copy, paste events with javascript codes,
$(document).ready(function(){  
$(document).keydown(function(event) {  
   //event.ctrlKey = check ctrl key is press or not  
   //event.which = check for F7  
   // event.which =check for v key  
   if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {  
      event.preventDefault();  
      }  
   });  
}); 
//Prevent browser Debugger console example,
$(document).keydown(function (event) {
// Prevent F12 -
if (event.keyCode == 123) {
   return false;
}
// Prevent Ctrl+a = disable select all
// Prevent Ctrl+u = disable view page source
// Prevent Ctrl+s = disable save
if (event.ctrlKey && (event.keyCode === 85 || event.keyCode === 83 || event.keyCode ===65 )) {
   return false;
}
// Prevent Ctrl+Shift+I = disabled debugger console using keys open
else if (event.ctrlKey && event.shiftKey && event.keyCode === 73)
{
   return false;
}
//print disable by ctrl+p
else if(event.ctrlKey && event.keyCode===80){
 return false;
  }
});

Using Javascript code you can block any event of browser.

Somen Das
  • 366
  • 2
  • 7