Search code examples
node.jsaws-lambdaaws-lambda-layers

Cannot read property 'filename' of undefined Using html-pdf with lambda


I'm experiencing problems when I try to create a PDF from HTML using Lambda Function, I'm receiving the error below:

{
    "errorType": "TypeError",
    "errorMessage": "Cannot read property 'filename' of undefined",
    "trace": [
        "TypeError: Cannot read property 'filename' of undefined",
        "    at execPdfToBuffer (/var/task/node_modules/html-pdf/lib/pdf.js:48:21)",
        "    at ChildProcess.respond (/var/task/node_modules/html-pdf/lib/pdf.js:144:5)",
        "    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)"
    ]
}

My code is:

try{
    
    let arq = await new Promise((res,rej)=>{
      pdf.create(html,{
        format: "Letter",
        orientation: "portrait",
        phantomPath: '/opt/phantomjs_linux-x86_64'
      }).toBuffer(function(err, buffer){
        if (err){
          rej(false);
        }else{
          res(buffer);
        } 
      });
    });

    const params = {
      Key: 'teste.pdf',
      Body: arq, // <---------
      Bucket: 'temp'
    };
    
    let S3 = new AWS.S3();
    let response = await S3.upload(params).promise();
    
    if (response){
      return true;
    }else{
      return false;
    }

  }catch(err){
    console.log(err);
    return false;
  }

I've read this topic: html-pdf package is not working on aws lambda But the proposed solution didn't work for me.

Thank you in advance


Solution

  • In the version 3.0.1 you can set the flag localUrlAccess: true, , it will work