Convert S3-website HTML to PF using Webkit (QtWebKit) on AWS Lambda.
Permissions required to properly function.
s3:PutObject
- Upload the output PDF to the buckets3:PutObjectTagging
- Set tags on the uploaded PDF
Note that s3:GetObject is not used. The html page is loaded directly from the s3-website URL which requires public read.
CloudWatch logging permissions. If you Create new role from template(s), these logging permissions will be automatically granted
logs:CreateLogGroup
logs:CreateLogStream
logs:PutLogEvents
All configuration is handled through Environment variables on the AWS Management Console. If not provided default values will be used.
Variable | Description | Type | Default Value |
---|---|---|---|
filename_filter |
Filename filter used by node.js for fine-grained filter | RegEx | .htm, .html |
page_size |
The size of PDF pages | String | 'letter' |
header_left |
Text to display left-aligned in the Header | String1 | Empty string |
header_center |
Text to display center-aligned in the Header | String1 | Empty string |
header_right |
Text to display right-aligned in the Header | String1 | Empty string |
footer_left |
Text to display left-aligned in the Footer | String1 | Empty string |
footer_center |
Text to display center-aligned in the Footer | String1 | Empty string |
footer_right |
Text to display right-aligned in the Footer | String1 | Empty string |
page_zoom |
Page scaling | Number | 1.00 |
disable_javascript |
Show JavaScript run on the webpage | Boolean2 | false |
print_media_type |
Use the "print" media type when generating the PDF | Boolean2 | false |
enable_forms |
Convert HTML form elements to PDF form elements | Boolean2 | false |
disable_external_links |
Do not load resources from remote webpages | Boolean2 | false |
no_background |
Do not print background | Boolean2 | false |
no_images |
Do not load or print images | Boolean2 | false |
grayscale |
Generate the PDF in grayscale | Boolean2 | false |
1 See wkhtmltopdf documentation > "Footers and Headers"
2 Valid boolean values: "true" or "false"
- Log into your AWS account
- Under Lambda, click Create a Lambda function button
- Select s3-get-object as the blueprint and click the Next button
- Configure Trigger
- Bucket - Select the S3-website bucket.
- Event type = Object Created (All)
- Prefix - Set based on your needs
- Suffix - Set based on your needs (Recommend: ".html")
- Check Enable trigger
- Click Next button
- Configure function
- Name - Enter name of your function (e.g. "s3html-to-pdf"),
- Runtime = Node.js 4.3
- Code entry type = Upload a .ZIP file
- Click Upload button to upload this project as zip archive.
- Set desired Environment variables to configure the function
- Role = Create new role from template(s)
- Role name - Enter name for the new role (e.g. "s3html-to-pdf-lambda-role")
- Policy templates - Remove the default S3 object read-only permission. Leave this field empty.
- Timeout (under Advanced settings) - Set to 10 seconds or more
- Click Next button
- Review your configuration and click Create function button
- Log into your AWS account
- Under IAM, click Policies in the left navigation pane
- Click Create Policy button
- Select Create Your Own Policy
- Policy Name - Enter the name of the policy (e.g. "s3-pdfUpload-policy")
- Policy Document - Use the policy document below
- Click Create Policy button
- Click Roles in the left navigation pane
- Choose the Lambda Role we created earlier (e.g. "s3html-to-pdf-lambda-role")
- Click Attach Policy button on the Permissions tab
- Select the policy we just created (e.g. "s3-pdfUpload-policy")
- Click Attach Policy button
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:PutObjectTagging"
],
"Resource": [
"arn:aws:s3:::*/*.pdf"
]
}
]
}
For the test we will be using the S3 Put sample event, changing only the parameters that are used by the function
- Click Actions button, then click Configure test event
- Select S3 Put
- In the JSON, update the following parameters
Records[0].s3.bucket.bucketname
- Enter your bucket's nameRecords[0].s3.object.key
- Enter an existing html webpage from your bucket- Click Save and test button. If your function is working correctly, you should receive following output:
The wkhtmltopdf
command does not have execute permissions.
Repackage zip to include POSIX execute permission (e.g. 755
) on this file
Resource returned HTTP 403 - Forbidden
error.
Ensure public read access on either bucket policy or ACLs.