简体   繁体   中英

html-pdf package is not working on aws lambda

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

Example file structure for the AWS X-Ray SDK for Node.js

`xray-sdk.zip └ nodejs/node_modules/aws-xray-sdk`

So you can verify that your layer zip file has the right structure.

In the options, set localUrlAccess: true , I got the same issue, and this resolved it.

var options = {
  height: "590px",
  width: "800px",
  phantomPath: '/opt/phantomjs_linux-x86_64',
  localUrlAccess: true
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM