Using PDFtk in AWS Lamba

August 4, 2018

If you're manipulating PDFs using AWS Lambda you'll probably need PDFtk at some point.

PDFtk is the Swiss army knife of PDF manipulation. PDFtk is preinstalled on Mac.

So locally on my computer I can get the number of pages in a PDF with this PDFtk command.

But obviously this won't work in AWS Lambda because PDFtk is not a preinstalled binary.

The work around is:

npm i pdftk-lambda

And then require the package at the top of your Lambda function:

// handler.js
require('pdftk-lambda')
module.exports.handler = (event, context) => {
  // ...
}

This package includes PDFtk prebuilt on Amazon Linux. You must require it at the top of your function to update process.env.PKG_CONFIG_PATH and process.env.LD_LIBRARY_PATH to point to the binary in node_modules/pdfktk-lambda/bin.