Facing error ERR_DLOPEN_FAILED using PDFTron node with AWS Lambda

Product: const { PDFNet } = require(“@pdftron/pdfnet-node”);

Product Version: “@pdftron/pdfnet-node”: “^9.3.0”

Please give a brief summary of your issue:
Facing an issue using PDFTron with AWS Lambda ran locally in my machine through AWS SAM

Error: {“code”:“ERR_DLOPEN_FAILED”}
code: 'ERR_DLOPEN_FAILED’orker (/var/task/node_modules/@pdftron/pdfnet-node/lib/pdfnet.js:1161:157)

Resources:
PDFTronUtilityFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: pdftron-app/
Handler: app.lambdaHandler
Runtime: nodejs16.x
Architectures:

  • x86_64

Code (with demo license key):

const main = async () => {
  const doc = await PDFNet.PDFDoc.create();
  const page = await doc.pageCreate();
  doc.pagePushBack(page);
  doc.save("/tmp/blank.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized);
};

PDFNet.runWithCleanup(
      main,
      "demo:1664106129913:7afc142c03000000005f3ab080e421962fa9c3cf0e2b90dd9379e82f34"
    )
      .catch(function (error) {
        console.log(error);
        console.log("Error: " + JSON.stringify(error));
      })
      .then(function () {
        PDFNet.shutdown();
      });

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

AWS Lambda ran locally in my machine through AWS SAM

Does this only happen in this case?
What if run the lambda normally?
What if you run one of our nodejs samples locally?

Does this only happen in this case?
Yes only when the PDF tron library is used. If I remove PDFtron library the Lambda runs fine.
What if run the lambda normally?
Lambda runs normally with Node js and core libraries like express or fs but fails with this error when PDFTron library is used
What if you run one of our nodejs samples locally?
I will try this and let you know, but I am trying to use PDFTron for AWS Lambda project build through SAM and executed locally on the docker Linux x86-64 container

I tried a sample code provided by PDFTron for AWS Lambda + Node js: -


https://github.com/PDFTron/aws-function-example/tree/main/server-nodejs

First time I ran the code (locally through SAM docker) it threw an error saying -

License key is required for function ‘PDFNet.runWithCleanup’.\nPlease go to PDFTron Systems Inc. | Pws to obtain a demo license or Contact Sales | PDFTron to obtain a production key.",“trace”:["Error: License key is required for function ‘PDFNet.runWithCleanup’

So, I generated a demo key and put that in the code.
My code:
https://github.com/SusriDey/pdftron-app

The code fails with the exact same error -

PS C:\AWS\SAM\pdftron-app> sam local invoke PdfUtilityFunction -e events/event.json
Invoking app.handler (nodejs16.x)
Skip pulling image and use local one: public.ecr.aws/sam/emulation-nodejs16.x:rapid-1.57.0-x86_64.

Mounting C:\AWS\SAM\pdftron-app\.aws-sam\build\PdfUtilityFunction as /var/task:ro,delegated inside runtime container
START RequestId: f4c282e5-3b1f-4112-95ee-89212765bd65 Version: $LATEST
2022-09-27T20:28:42.347Z        f4c282e5-3b1f-4112-95ee-89212765bd65    INFO    Error: {"code":"ERR_DLOPEN_FAILED"}
END RequestId: f4c282e5-3b1f-4112-95ee-89212765bd65
REPORT RequestId: f4c282e5-3b1f-4112-95ee-89212765bd65  Init Duration: 0.17 ms  Duration: 524.16 ms     Billed Duration: 525 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode":500,"body":"{\"code\":\"ERR_DLOPEN_FAILED\"}"}

to run the code one needs to set up AWS SAM locally and run the below commands:

sam build
sam local invoke PdfUtilityFunction -e events/event.json

We have many customers using our SDK in AWS Lambda’s. So it appears that somehow in this setup the libPDFNetC.so library is either not being found at all, or its blocked somehow.

Please check the documenation for AWS SAM NodeJS on how to allow the runtime to load/read a native shared library .so file. Depending on the exact framework sometimes they need to be placed in a certain file, or registered in a config file of some sort.

Another possiblity is mixed up nodejs versions. If your lambda is Nodejs16 make sure that is how you are packaging it locally before deployment.