19

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?

7
  • 6
    I'd really like to hear the rational for this request. Commented Jul 11, 2011 at 8:16
  • 1
    @roXon OK, I take all my snarkyness back. Great cause! :D Commented Jul 11, 2011 at 8:21
  • 2
    @Gourav You're forgetting File > Print. Commented Jul 11, 2011 at 8:22
  • 2
    FYI, there are numerous of printscreen programs that allow taking screenshot of full pages easily and then print them. @roXon hehe.. +1 Commented Jul 11, 2011 at 8:23
  • 1
    @roxon, download a spyware/virus on user's machine and burn down the hard drive. Nothing to print. Commented Jul 11, 2011 at 10:31

12 Answers 12

29

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 - 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.
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.
24

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.

5 Comments

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.
+1 yeeeah I like the Mad Max part '...user's machine is out of your control.'
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.
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.
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. stackoverflow.com/questions/6647392/…
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

Comments

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.

Comments

5

Add such block to your CSS

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

This should hide all content when printing.

3 Comments

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.
Of course you can't secure such cases. User can also capture the screen using camera you know.
this can be changed from browser inspection
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.

Comments

2

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

1 Comment

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?
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.

Comments

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.

2 Comments

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 Yes, you are right. My answer is applicable to only body of html but your answer is covering all window.
1

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.

2 Comments

Please fix your comments. Like this the code neither readable nor runnable.
Now check the code i think it will fine now
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?

Comments

0

Several people on here are asking why such a feature on a web page is useful for.

First of all, nothing is truly secure. Anything can be hacked is only a matter of time. That stated, stopping printing of content on a page can be made more difficult, at best, technically speaking.

Another way is to have a click through that they will agree not print it's contents AND/OR use them in any court case. If you have a lawyer write it, that too will only slow them down.

Not being able to use the documents of a site in a court case is likely to gum up the workings of the court, making it expensive for the other side. But that's the point.

If you can't make it impossible, them make it difficult and expensive. Having been in many extensive legal battles with government, slowing them down is a very real legal tactic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.