Unable to decrypt PDF in node using node-qpdf

2k Views Asked by At

We need to decrypt the password protected PDF file using node. We are using the node-qpdf. Here is our scenario.

1). We are getting the password protected PDF file as input. 2). using the QPDF , with available password ,we are trying to decrypt it. 3). At the end we are getting the corrupted PDF file.

We are following the given link for reference : a). https://www.npmjs.com/package/node-qpdf

Under decryption section, we can see the below code :

var qpdf = require('node-qpdf');
qpdf.decrypt(localFilePath, 'YOUR_PASSWORD_TO_DECRYPT_PDF', outputFilePath);

localFilePath = './myInputFile_WithPassword.pdf'
YOUR_PASSWORD_TO_DECRYPT_PDF = 'test'
outputFilePath = './myoutfileWith_NopasswordProtection.pdf'

After running the same we get the error as :

TypeError: callback is not a function
at handleError (/home/runner/node_modules/node-qpdf/index.js:141:5)
at Socket.<anonymous> (/home/runner/node_modules/node-qpdf/index.js:124:5)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at Socket.EventEmitter.emit (domain.js:448:20)
at addChunk (_stream_readable.js:287:12)
at readableAddChunk (_stream_readable.js:268:11)
at Socket.Readable.push (_stream_readable.js:223:10)
at Pipe.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

For callback issue we have also tried to change the code as :

    qpdf.decrypt(localFilePath,'YOUR_PASSWORD_TO_DECRYPT_PDF', (res)=>{
    console.log(res);
    fs.createWriteStream(outputFilePath)       
     });

But getting error as :

"**Error: spawn qpdf ENOENT**"

We have also tried the following code :

============

var exec = require('child_process').exec;
var pdfsourcepath = './myInputFile_WithPassword.pdf'; 
var pdfdestinationpath = './myoutfileWith_NopasswordProtection.pdf'; 
var password = "test";  

var command = 'qpdf --decrypt --password='+password+' '+pdfsourcepath+' '+pdfdestinationpath+'';

exec(command, function (error){
if (error !== null){
console.log('exec error: ' + error);
 } 
else{
    console.log('Your pdf is decrypted successfully.');
    }
   }
 );

============

And this code is working successfully.

Now We want to totally implement the same using node module [node-qpdf], but that is failing [as explained].

Please share your idea on the issue.

Also we implement the same on docker images, so if we will install the qpdf on main server, does the containers will able to get that binary ? I have checked the article : https://hub.docker.com/r/mgodlewski/qpdf But I get a little bit confused.

So also share the resolution in terms of docker images for qpdf.

1

There are 1 best solutions below

0
On

I think just remove the callback parameter. The decrypt command may return an output stream of the decrypted file.