I have tried implementing html-pdf package in my code which is deployed at AWS lambda but getting error in it even after I have deployed the layer for html-pdf package at lambda. Below is my code:
import pdf from 'html-pdf';
import AWS from 'aws-sdk';
var S3 = new AWS.S3();
process.env.PATH = `${process.env.PATH}:/opt`;
process.env.FONTCONFIG_PATH = "/opt";
process.env.LD_LIBRARY_PATH = "/opt";
export const convertToPDFandUpload = async (attachmentFile, empCertificate) => {
let file;
let params;
var options = {
height: "590px",
width: "800px",
phantomPath: '/opt/phantomjs_linux-x86_64'
};
return new Promise((resolve, reject) => {
pdf.create(attachmentFile, options).toBuffer(function (err, res) {
if (err)
console.log(err);
file = res;
params = {
Bucket: <my_bucket_name>,
Key: `certificate/${empCertificate.candidateName}${empCertificate.certificateID}.pdf`,
Body: file,
ACL: 'public-read'
};
S3.upload(params, async function (err, data) {
if (err) {
console.log(err, err.stack);
reject(null);
} else {
resolve(data.Location);
}
});
});
});
};
It is throwing an error after toBuffer() function.
Error: { "errorType": "TypeError", "errorMessage": "Cannot read property 'filename' of undefined", "stack": [ "TypeError: Cannot read property 'filename' of undefined", " at execPdfToBuffer (/var/task/apis/webpack:/home/nikhilsrivastva/Desktop/HR Onboarding/onboarding BE/hronboardingcodebase/services/certification/node_modules/html-pdf/lib/pdf.js:48:1)", " at ChildProcess.respond (/var/task/apis/webpack:/home/nikhilsrivastva/Desktop/HR Onboarding/onboarding BE/hronboardingcodebase/services/certification/node_modules/html-pdf/lib/pdf.js:144:1)", " at ChildProcess.emit (events.js:314:20)", " at ChildProcess.EventEmitter.emit (domain.js:483:12)", " at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)" ] }
The layer zip file must contain a folder named "nodejs" and that folder must contain the "node_modules" folder that has your dependencies. View the exact structure here: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
So you can verify that your layer zip file has the right structure.