Lambda function to create a .pdf file from HTML and store it on S3
- Download project
- Run
npm install - Create a .zip file containing this project
- Create an AWS Lambda function with a NodeJS runtime (tested from version 10.x+)
- Associate the function with an executing role that has
AWSLambdaBasicExecutionRolepolicy and writing permissions over the desired S3 buckets - Add two layers to the Lambda function (change region on the ARN depending on the function's region):
- arn:aws:lambda:[REGION]:347599033421:layer:wkhtmltopdf:1
- arn:aws:lambda:[REGION]:347599033421:layer:amazon_linux_fonts:1
- Add
FONTCONFIG_PATHas an environment variable with value/opt/etc/fonts - Change function's timeout to something that works for you (I use 25 seconds)
- Upload .zip file from step 3 as the function's code
This function receives the following payload:
- html: String representation of the HTML code to process
- location: Directory to store produced file. Must end with '/'
- filename: .pdf file name without extension
- s3_bucket: Name of the bucket to store the file. Must have writing permissions
- *options**: Object with specific options from wkhtmltopdf's docs
* Optional
client = Aws::Lambda::Client.new(region: region_name)
resp = client.invoke({
function_name: 'html-to-pdf',
payload: '{
"html": "<html><h1>Hello, world!</h1></html>",
"location": "pdfs/",
"filename": "test",
"s3_bucket": "my-pdfs-bucket"
}'
})