Welcome folks today in this blog post we will be encrypting
pdf documents with password using the qpdf
library in command line using child process
. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the qpdf
library globally inside your system on the command line
npm i -g qpdf
After that you need to use this library using the below command as shown below
1 |
qpdf --encrypt user-password key-length flags -- source-file-path destination-file-path |
As you can see we are using the above command here you need to replace the path of the input
pdf file and output
pdf file. And here you need to replace the password
you need to encrypt. This command will translate into as shown below
qpdf --encrypt test 40 sample.pdf encrypted.pdf
And now you can make the index.js
file inside the node.js project using the child_process()
module as shown below
index.js
1 2 3 4 5 6 7 8 9 10 11 12 |
let exec = require('child_process').exec let cmd = "qpdf --encrypt test 40 sample.pdf encrypted.pdf" exec(cmd,(err) => { if(err){ console.log(err) } else{ console.log("PDF encrypted") } }) |