Generating PDFs with Node, PDFkit, and Serverless on AWS Lambda

There are a few blog posts out there already covering this subject, but most of them include extra packages or steps than are actually necessary. In this post, I’ll cover only the minimum to create a Serverless function on AWS Lambda which generates PDFs using Node and PDFKit. No need for Express, no HTML parsing, and no uploading to S3.

Setting up AWS Lambda with Serverless

Getting started with serverless functions with Node on AWS Lambda is pretty straight forward. We will need to use a configuration file called (for more details on the file options, see here). This file should look something like so:

This configuration assumes we have a function called which is exported from the file called located inside a folder called .

Each serverless function handler is provided with 3 parameters:

  1. The request “" containing all sorts of details like the route requested, the request method, the request headers, and more.
  2. The lambda “" which provides details about the context of the function, as well as some methods for the response.
  3. A Node.js style, error-first" function used to send back the response data.

Here is a very basic handler example. Note that the callback function expects an object for the response ( not a Response object), which must have a “ " key:

If you’re like me and prefer promises, we can convert it to use async/await like so:

Cool. Our basic lambda function is working.

Generating PDFs in Node with PDFKit

Next, we will look at generating a PDF in Node. There are a few options, but the one I found most common was PDFKit. You can install it in your project with “ ".

A basic “hello world” example for generating a PDF in memory requires us to use buffers. It looks something like this:

This is fine, but since we are using async/await, we want to use a Promise instead of a callback:

Adding PDFs as a serverless response

We’re almost done. We have a serverless endpoint that runs a Node function, and we have a Node function that generates a PDF. However, there is a little bit of configuration needed in the AWS Gateway in order to server PDFs to the browser as binary data.

First, we need to install the Serverless plugins serverless-apigw-binary and serverless-apigwy-binary (it’s not a typo, they are close, but not the same). We can do so with .

With those installed, we also need to make a few changes to our file. We need to tell the AWS Gateway to include binary media types, tell our function to serve the content as binary, include the aforementioned plugins, and tell AWS which content type to serve as binary based on the HTTP header it receives:

With that in place, we can edit our previous “hello world” serverless function to use the PDFKit generation. We also have to make sure to encode our PDF buffer, send the appropriate "application/pdf" response header, and set the flag for the response to :

Originally published at https://stegosource.com on November 2, 2019.

Full Stack @Revealbio | Author of Vuetensils & Particles CSS | Panelist @ViewsOnVue | Board Member @sandiegojs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store