AWS Lambda PDF Generator
·
647 words
·
4 minutes read
This is an example of how to convert HTML code into a PDF using AWS’ Lambda service and S3 Triggers. So that once a HTML file is upload to S3 it will automatically be converted into a PDF which should appear in the same bucket shortly after - all using a serverless function.
To do this, other than the standard library, we’re using a Go wkhtmltopdf library and the Go AWS sdk. The wkhtmltopdf Go library is a wrapper to the binary file - which will need to be included in our final zip file that we upload to lambda.
|
|
For this to work, you’ll need to:
- Name the code above
go-pdf-lambda.go
and rungo build go-pdf-lambda.go
to make the binary file. - Include a copy of wkhtmltopdf along with your lambda file inside the zip (download directly from here or from their download page).
- Make sure
wkhtmltopdf
is set to executable withchmod +x wkhtmltopdf
.
And on AWS:
- The lambda function name doesn’t matter, but it will need to be set to the Go 1.x runtime.
- Set your Lambda root Handler to the go binary filename (
go-pdf-lambda
in our example) - Add an S3 trigger on all create events.
- Give your lambda function/role read & write permissions to S3 (through IAM).
How the zip file should look: