How to disable printing on a website
Tags: javascript, css, webdev, privacy
Published: 2022-09-19 09:05:32

Sometimes, we have some projects where the content is sensitive or should be protected for privacy reasons or copyrights. A quick solution involves:

- To prevent the key combination **Ctrl + P** (for printing) _via JS_.
- To prevent text selecting and copying text _via JS_.
- To hide the whole page on printing _via CSS_.

```html
<script>
    //Prevents Ctrl + P
    //From https://stackoverflow.com/a/47848090/1928691
    window.addEventListener('keydown', function(event) {
        if (event.keyCode === 80 && (event.ctrlKey || event.metaKey) && !event.altKey && (!event.shiftKey || window.chrome || window.opera)) {
            event.preventDefault();
            if (event.stopImmediatePropagation) {
                event.stopImmediatePropagation();
            } else {
                event.stopPropagation();
            }
            return;
            }
    }, true);

    //Disables text selection and copying
    //From https://gist.github.com/pbaruns/a24ad79b027956ed5d77fd3e6c201467
    if (typeof document.onselectstart != "undefined") { 
        document.onselectstart = new Function ("return false");
    } 
    else { 
        document.onmousedown = new Function("return false"); 
        document.onmouseup = new Function("return true");
    }
</script>

<style>
    /*From https://stackoverflow.com/a/3359980/1928691*/
    @media print {
        html, body {
            display: none;  /* hide whole page */
        }
    }
</style>
```

**Is it perfect?** Of course, is not.
- You might need to add a **paywall** to prevent its access.
- You might need to add a **DRM solution** to prevent _screenshots_ (that are often followed by _OCRs_ to extract their text). _Udemy does this already, you cannot take screenshots from several videos. You can take them if you use Firefox or less common browsers._
- You will create accessibility issues.
- The **reader mode** often bypasses common restrictions.
- Some people can bypass it using the **Dev Console** and copy the text from the tags _(Devs, RPAs, or geeks mainly?)_.
- You might need to **encrypt** the text using a **DRM service** for anyone using the Dev Console.

This is just a quick solution that can support you in your journey.

### Follow me on:

|  Personal  |  LinkedIn  |YouTube|Instagram|Cyber Prophets|Sharing Your Stories|
|:----------|:----------|:------------:|:------------:|:------------:|:------------:|
|[![Personal](https://raw.githubusercontent.com/FANMixco/federiconavarrete/master/img/favicons/favicon.png)](https://federiconavarrete.com)|[![LinkedIn](https://i.stack.imgur.com/idQWu.png)](https://www.linkedin.com/in/federiconavarrete)|[![YouTube](https://i.stack.imgur.com/CFPMR.png)](https://youtube.com/c/FedericoNavarrete)|[![Instagram](https://i.stack.imgur.com/PIfqY.png)](https://www.instagram.com/federico_the_consultant)|[![RedCircle Podcast](https://i.stack.imgur.com/4XICF.png)](https://redcircle.com/shows/cyber-prophets)|[![RedCircle Podcast](https://i.stack.imgur.com/4XICF.png)](https://redcircle.com/shows/sharing-your-stories)|

[![sponsor me](https://raw.githubusercontent.com/FANMixco/Xamarin-SearchBar/master/bmc-rezr5vpd.gif)](https://www.buymeacoffee.com/fanmixco)