Reputation: 6936
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
Upvotes: 0
Views: 14359
Reputation: 149
I know Raju asked about Node, but since Bun is now out and compatible with Node, I want to show how it's to use qpdf
from karthick's answer with Bun.
import { $ } from "bun";
const cmd = 'qpdf --encrypt test test 40 -- Downloads/1.pdf Downloads/encryptpdfvianode.pdf';
try {
await $`${cmd}`;
console.log('PDF encrypted :)');
} catch (err) {
console.error('Error occurred: ' + err);
}
You can also get output with it: Here you can find some docs on $
Upvotes: 0
Reputation: 6168
Yes, It is possible to Encrypt
PDF in nodejs
using QPDF.
Install QPDF on your machine/server using following command
sudo apt-get install qpdf
or
brew install qpdf
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.
You can do the same in nodejs using child process or shelljs
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.
Upvotes: 2