Skip to content

HTML-to-PDF API with Handlebars support, hosted on AWS Lambda

License

Notifications You must be signed in to change notification settings

Sleavely/pdf-lambda

Repository files navigation

pdf-lambda

An AWS Lambda that renders PDFs from HTML templates with the help of Handlebars and puppeteer.

Setup

Deploy the Cloudformation stack to AWS:

make deploy

This will create an API gateway and an S3 bucket for your PDFs.

Usage

The API exposes a single POST endpoint:

/pdf-lambda

Parameters

  • url - The URL to use as a template.
  • data - A JSON object to feed Handlebars

Description

Request a render from the API endpoint:

curl \
  -X POST \
  -H "Content-Type: application/json" \
  https://your-api-gateway-hostname/pdf-lambda \
  -d '{
    "url": "https://docs.google.com/document/d/e/2PACX-1vRsOF1-sr6U5ONvX6rvk3SYJII2Qf3SlCyj-DD1gNYpH7h3D1ut-rXd_kjphC0Fs9_GsCTAM5Xl8vrH/pub",
    "data": {
      "name": "Sleavely",
      "experiences": [
        { "company": "Stark Industries", "title": "Tech Lead", "description": "Making tiny iron people." },
        { "company": "Wayne Enterprises", "title": "Security Officer", "description": "Security officer by day, millionaire by night."},
        { "company": "Los Pollos Hermanos", "title": "Clerk", "description": "I took a sabbatical to fry some chicken. Learned a lot about chemistry."},
        { "company": "Gringotts", "title": "Financial Advisor", "description": "Improved securiy with KMS"}
      ]
    }
  }'

The API will return an S3 URL for the rendered PDF.

Here's a before- & after- using the cURL from earlier: