简体   繁体   中英

PDF encryption using nodejs

I have a requirement for creating pdf from existing pdf. Where an existing pdf is copied into a new pdf and new pdf will be password protected(file open password).

I can do it using PHP mpdf. Just want to know if it is possible with nodejs.

Requirements are simple:

1- Copy existing pdf into new pdf. 2- Password protect new pdf.

Thanks

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM