-1

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

3
  • Your question is open ended without one possible single answer other than "yes" and can have far too many possible answers. stackoverflow.com/help/how-to-ask Commented Sep 13, 2017 at 14:23
  • Can you please suggest a nodejs library having these options, just like mpdf in php Commented Sep 13, 2017 at 14:34
  • Asking for library suggestions is not allowed on SO. Commented Sep 13, 2017 at 18:08

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

How can I encrypt a pdf create using html-pdf like a buffer ?
Dont USE QPDF IF YOU HAVE A WINDOWS SYSTEM
Thank @Hector. I have struggling with node-qpdf for some time but had not installed qpdf yet, so sudo apt-get install qpdf did the trick. @ShashankSaxena why not? Does that apply to WSL as well?
same question @Hector, is there a way to encrypt a generated pdf buffer
I made this creating a pdf as buffer using const buffer = await pify(pdf.create(html, this.options)).toBuffer(); then encrypt using createWriter and PDFRStreamForBuffer from npm package called muhammara
|
0

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 $

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.