wobsoriano
wobsoriano

Reputation: 13462

Add password to PDF's automatically

I'm currently using this code to just get an alert in the print dialog box

(function() {

    var beforePrint = function() {
        alert('Functionality to run before printing.');
    };

    var afterPrint = function() {
        alert('Functionality to run after printing');

    };

    if (window.matchMedia) {
        var mediaQueryList = window.matchMedia('print');
        mediaQueryList.addListener(function(mql) {
            if (mql.matches) {
                beforePrint();
            } else {
                afterPrint();
            }
        });
    }

    window.onbeforeprint = beforePrint;
    window.onafterprint = afterPrint;

}());

Everything works, I get an alert before and after the print dialog box shows.

Now my problem is, or what I wanted to do, is to automatically secure saved pdf with passwords like I will set a random password in the script and the pdf will automatically be secured with that password.

Is that even possible? Thank you in advance.

Upvotes: 0

Views: 2033

Answers (1)

Eugene
Eugene

Reputation: 2878

It is not possible to secure PDF using client side javascript. Your options are:

  • secure pdf file on server using server-side library that can take existing pdf and apply a password to it;
  • if you can regenerate PDF then you may try to implement javascript code (inside PDF) to check for passwords. This method is less secure because it relies on your own code and not utilizing PDF format built-in security features (and encryption);

Upvotes: 1

Related Questions