This is an AWS Lambda function that converts HTML pages to PDF documents using wkhtmltopdf (0.12.4). It implements a simple interface to read HTML input and output PDF content.
Input event to this function has the following structure:
{
"html": "<!DOCTYPE html><html><head><title>HTML doc</title></head><body>Content</body></html>"
}
It yields a response in the following format:
{
"data": "JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7..."
}
data is base64 encoding of the converted PDF file.
The function can be tested locally using AWS SAM CLI. You can change contents of events/example-event.json or you can create a new file which you will give sam as an event parameter.
sam local invoke "HtmlToPdf" -e events/example-event.json
There are two ways in which these functions can be deployed to AWS.
1 - Check our npm run deploy:dev and npm run deploy:prod commands in package.json and change it according to your needs. Do not forget to add environment variables (you can find it under template.yml) to your lambda function in aws lambda edit page or running lamba update-function-configuration command. Be sure to create the following environment variable in Lambda: FONTCONFIG_PATH=/var/task/fonts
2 - Check out template.yml file and edit according to your needs then use sam deploy.
Provide the related font file under the fonts directory to support specific languages and characters.
- You must use v2.x.x tag if you are using Node.js runtime >=10.x in AWS Lambda.
- For Node.js >18.x in AWS Lambda, external images may fail to render in the generated PDF. To ensure images display correctly, embed them as base64 data URIs in your HTML instead of using external URLs.