/aws-lambda-wkhtmltopdf

Convert HTML to PDF using Webkit (QtWebKit) on AWS Lambda

Primary LanguageJavaScript

aws-lambda-wkhtmltopdf

Convert HTML to PDF using Webkit (QtWebKit) on AWS Lambda

Creating function under your AWS account

  1. Log into your AWS account
  2. Under Lambda, click Create a Lambda function button
  3. When being asked to select blueprint, do not select any and click Skip button instead
  4. Do not configure any triggers and simply click Next button
  5. Enter name of your function (e.g. "html-to-pdf"), then select runtime Node.js 4.3
  6. In Code entry type change Edit code inline to Upload a .ZIP file
  7. Click Upload button to upload this project as zip archive.
  8. Select lambda_basic_execution role (this function doesn't need access to any part of your AWS account)
  9. Increase timeout from 3 seconds to 10 seconds or more.
  10. Click Next button
  11. Review your configuration and click Create function button

Testing function

Click Actions button, then click Configure test event and paste following json input:

{
    "html_base64" : "PGJvZHk+SGVsbG8gd29ybGQ8L2JvZHk+"
}

html_base64 parameter represents encoded <body>Hello world</body>

Then click Save and test button. If your function is working correctly, you should receive following output:

{
  "pdf_base64": "JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/KQovQ3JlYXRvciAo/v8AdwBrAGgAdABtAGwAdABvAHAAZABmACAAMAAuADEAMgAuADMALQBkAGUAdgAtADcAOQBmAGYANQAxAGUpCi9Qcm9kdWNlciAo/v8AUQB0ACAANAAuADgALgA3KQovQ3JlYXRpb25EYXRlIChEOjIwMTYxMDAxMDQzMDU5WikKPj4KZW5kb2JqCjMgMCBvYmoKPDwKL1R5cGUgL0V4dEdTdGF0ZQovU0EgdHJ1ZQovU00gMC4wMgovY2EgMS4wCi9DQSAxLjAKL0FJUyBmYWxzZQovU01hc2sgL05vbmU+PgplbmRvYmoKNCAwIG9iagpbL1BhdHRlcm4gL0RldmljZVJHQl0KZW5kb2JqCjcgMCBvYmoKPDwKL1R5cGUgL0NhdGFsb2cKL1BhZ2VzIDIgMCBSCj4+CmVuZG9iago1IDAgb2JqCjw8Ci9UeXBlIC9QYWdlCi9QYXJlbnQgMiAwIFIKL0Nvbn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MDAwMDAwMjI2NCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDE3Ci9JbmZvIDEgMCBSCi9Sb290IDcgMCBSCj4+CnN0YXJ0eHJlZgozMjQ2CiUlRU9GCg=="
}

The function will respond with PDF encoded in Base64

Options

There are many options available to wkhtmltopdf. All of the command line options are supported as documented on the page linked to above. The options are camelCased instead-of-dashed as in the command line tool. Note that options that do not have values, must be specified as a boolean, e.g. debugJavascript: true

{
    "html_base64" : "PGJvZHk+SGVsbG8gd29ybGQ8L2JvZHk+",
    "options":
    {
        "pageSize": "letter"
    }
}