├── .gitignore ├── README.md ├── bin ├── libgcj.so.10 └── pdftk ├── dist.sh ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Distribution Files 2 | *.zip 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda + PDFtk Example 2 | 3 | This repository provides a working example of using PDFtk within AWS Lambda. AWS Lambda runs on Amazon Linux, which does not officially support PDFtk or GCJ, one of PDFtk's dependencies. This example works by including both a PDFtk binary and the libgcj shared library. 4 | 5 | ## Run the Example 6 | 7 | To run the example, first package up the project into a ZIP file by running: 8 | 9 | ``` 10 | ./dist.sh 11 | ``` 12 | 13 | Then, simply upload this ZIP to AWS Lambda. When testing with the Lambda web interface, you should see the function succeed and output PDFtk's version and copyright information. 14 | 15 | You can very easily expand on this boilerplate and use PDFtk in the way it was intended for - manipulating PDF files. 16 | 17 | ## How it Works 18 | 19 | AWS Lambda supports binary dependencies by allowing them to be included in uploaded ZIP files. However, because Amazon Linux does not support PDFtk or GCJ, PDFtk was built from source in CentOS, a close relative of Amazon Linux. I spun up a CentOS 6 machine in EC2 and followed the instructions on the [PDFtk website](https://www.pdflabs.com/docs/install-pdftk-on-redhat-or-centos/) to build PDFtk from source. 20 | 21 | ``` 22 | sudo yum install gcc gcc-java libgcj libgcj-devel gcc-c++ 23 | 24 | wget https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-2.02-src.zip 25 | 26 | unzip pdftk-2.02-src.zip 27 | 28 | cd pdftk-2.02-dist/pdftk 29 | 30 | make -f Makefile.Redhat 31 | 32 | sudo make -f Makefile.Redhat install 33 | ``` 34 | 35 | Then I copied the resulting `pdftk` binary and `/usr/lib64/libgcj.so.10` shared library into the `bin/` directory of my Lambda project. 36 | 37 | The entry point to the lambda function, `index.js`, alters the `PATH` and `LD_LIBRARY_PATH` environment variables to let the system know where to find the binary and the GCJ dependency. 38 | 39 | ## Using PDFtk in Amazon Linux 40 | 41 | It should be possible to use the PDFtk binary and GCJ shared library located in the `bin/` directory of this file to run PDFtk in Amazon Linux on EC2. Simply copy them onto the machine and put them in the correct path, or call them directly: 42 | 43 | ``` 44 | LD_LIBRARY_PATH=/path/to/libgcj.so.10 /path/to/pdftk --version 45 | ``` 46 | -------------------------------------------------------------------------------- /bin/libgcj.so.10: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lob/lambda-pdftk-example/7c40f3c9588cfc3acb4d21355e8e349bd882041d/bin/libgcj.so.10 -------------------------------------------------------------------------------- /bin/pdftk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lob/lambda-pdftk-example/7c40f3c9588cfc3acb4d21355e8e349bd882041d/bin/pdftk -------------------------------------------------------------------------------- /dist.sh: -------------------------------------------------------------------------------- 1 | rm pdftk-example.zip 2 | zip pdftk-example.zip index.js bin/* 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var exec = require('child_process').exec; 4 | 5 | // Set the PATH and LD_LIBRARY_PATH environment variables. 6 | process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/bin'; 7 | process.env['LD_LIBRARY_PATH'] = process.env['LAMBDA_TASK_ROOT'] + '/bin'; 8 | 9 | exports.handler = function (event, context) { 10 | exec('pdftk --version', context.done); 11 | }; 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lambda-pdftk-example", 3 | "version": "0.1.0", 4 | "description": "Example project that runs PDFtk in AWS Lambda.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/lob/lambda-pdftk-example.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/lob/lambda-pdftk-example/issues" 17 | }, 18 | "homepage": "https://github.com/lob/lambda-pdftk-example" 19 | } 20 | --------------------------------------------------------------------------------