Reputation: 65
I am generating the PDF using html-pdf node library and then encrypting it using node-qpdf library.
pdf.create(htmlString, {format: 'A4', timeout: 600000})
.toFile(filePath, function (err, data) {
if (err) {
logger.error('Error while creating the pdf file ::->', err.stack);
return reject(err);
} else {
logger.info('Successfully able to create the file in tmp');
encrypt(filePath, '/tmp/' + _.get(lead, 'code', '00003') + '_receipt_encrypted.pdf');
return resolve(data);
}
});
var encrypt = function (inputFile, outputFile) {
let exec = require('child_process').exec;
var ownerPass = 'ownerpasswd'
var userPass = 'ownerpasswd'
let cmd = 'qpdf --encrypt ' + ownerPass + ' ' + userPass + ' 256 -- ' + inputFile + ' ' + outputFile
exec(cmd, function (err) {
if (err) {
console.error("The error", err, err.stack)
} else {
console.log("The PDF is encrypted")
}
})
}
I am able to generate the enccrypted pdf file in the /tmp/ folder bit when I am trying to send it via Email, I am getting the error
[Error: ENOENT: no such file or directory, open '/tmp/A6rz2K-p2n_receipt_encrypted.pdf'] {
Here is the code for reading file and sending it via email.
if (err) {
console.log('error in reading encrypted file================= ', err, err.stack);
}
var attachment = {
content: data.toString('base64'),
name: encryptedFileName,
type: "application/pdf"
};
sendReceiptEmail(lead,attachment);
logger.log("end of receipt pdf creation");
})
return Promise.resolve();
})```
Upvotes: 0
Views: 324
Reputation: 61
I guess you're having an issue with async code execution. The call to encrypt
looks good, since that function is called inside the pdf..toFile
callback function. Maybe you need to make sure sendReceiptEmail
is called after encrypt
is done, inside the exec
callback function.
As Raya commented, I'd suggest to refactor the code to use promises or async code execution as well.
Upvotes: 1