Yes, It is possible to Encrypt PDF in nodejs using QPDF.
Steps:
1.Install :
Install QPDF on your machine/server using following command
sudo apt-get install qpdf
or
brew install qpdf
2.Check whether it is working or not
qpdf --encrypt user-password owner-password key-length flags -- source-file-path destination-file-path
For example:
qpdf --encrypt test test 40 -- Downloads/1.pdf Downloads/encrypted.pdf
Now,
i.Try to open the encrypted.pdf file in the Downloads folder.
ii. It will ask for password, Enter password test which is given while encrypting the PDF file. Now you can able to open the file which means QPDF is working.
How to do it in nodejs?
You can do the same in nodejs using child process or shelljs
Code:
var exec = require('child_process').exec;
var cmd = 'qpdf --encrypt test test 40 -- Downloads/1.pdf Downloads/encryptpdfvianode.pdf';
exec(cmd, function (err){
if (err){
console.error('Error occured: ' + err);
}else{
console.log('PDF encrypted :)');
}
});
Note: You can also look at node-qpdf npm package.