신상택
신상택

Reputation: 31

PDF file in lambda nodejs

I have a PDF file in S3. eg. original.pdf

  1. use s3.getObject to get information of original.pdf
  2. generate an empty new pdf file ex)new.pdf
  3. i want to copy contents of original.pdf to new.pdf
  4. want to put the password in new.pdf
  5. use s3.putObject to upload new.pdf

please help me

    const AWS = require('aws-sdk');
    const PDFLIB = require('pdf-lib');
    const s3 = new AWS.S3();

    exports.handler = (event,context,callback) => {

        let params = {
            Bucket : 'bucket-name',
            Key : 'key'
        };

        s3.getObject(params,(err,data)=>{
            if(err){
                console.log(err,err.stack);
            }else{
                console.log(data);
            }
        });

        const pdfDoc = PDFLIB.create();
    }

i think i made step 1 and then i want to generate a new pdf for copying the original pdf.

but in lambda there is an error

Response:
{
  "errorType": "TypeError",
  "errorMessage": "PDFLIB.create is not a function",
  "trace": [
    "TypeError: PDFLIB.create is not a function",
    "    at Runtime.exports.handler (/var/task/test2.js:20:27)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

Upvotes: 0

Views: 1657

Answers (1)

Ashish Modi
Ashish Modi

Reputation: 7770

You need to extract the PDFDocument while requiring pdf-lib. check the documentation for more details. the code will be like this

const AWS = require('aws-sdk');
const { PDFDocument} = require('pdf-lib');
    const s3 = new AWS.S3();

    exports.handler = (event,context,callback) => {

        let params = {
            Bucket : 'bucket-name',
            Key : 'key'
        };

        s3.getObject(params,(err,data)=>{
            if(err){
                console.log(err,err.stack);
            }else{
                console.log(data);
            }
        });

        const pdfDoc = PDFDocument.create(); // this is a async function as per documentation so need to await it or use then after that.
    }

Hope this works. Note that create method is async as per documentation.

Upvotes: 1

Related Questions