Generating a PDF with Express & Node.JS

Koen van Gilst
Nov 3, 2016 · 2 min read
Image for post
Image for post

This article was also published on my own personal blog.

In this article you’ll find a fun little tutorial of how to create a PDF file using a web form, Express and PDFKit. To get started it’ll use the express generator and after completing it you’ll have a simple web form that generates a PDF file if the user clicks on the button Create PDF. I’ll also point you in the right direction if you want to deploy the app using now.

First install the express application generator:

yarn global add express-generator

Then use this to generate a basic Express app:

express —-view=pug form-to-pdf

Now go into this folder and install the dependencies using yarn:

cd form-to-pdf
yarn

Next add the dependency PDFKit

yarn add pdfkit

You can now start your app, using:

yarn start

Then go to http://localhost:3000 in your browser. You should see something like: “Welcome to Express”.

Next we’re going to add our HTML using the templating engine Pug (formerly known as Jade). Open the file views/index.pug and replace the existing code with the following:

We also want to add some styling using Bootstrap and a theme from Bootswatch. In views/layout.pug add the following link directly above the existing link to style.css:

Now if you restart your app and visit http://localhost:3000 you should see a nicely formatted HTML form. Next we’re going to add the route for creating PDF files. Create the file routes/pdf and add the following code:

Then open app.js and require the new pdf route file just below the existing routes and add it as a route:

Your PDF rendering Express application is now ready for use! Start it with:

yarn start

I hope you enjoyed this tutorial and feel free to ask me any questions. You can find me on Twitter as @vnglst.

You can also deploy your own Form to PDF app using now by Zeit.co. It’s easy and takes only a few seconds. Instructions can be found here: https://zeit.co/now

Thanks to Samer Buna for reviewing this!

EdgeCoders

Koen van Gilst

Written by

JavaScript Developer | M.A. in Philosophy | https://github.com/vnglst

EdgeCoders

jsComplete’s Medium Publication — jsComplete is a FREE software educational library focused on JavaScript, Node, React & GraphQL. It has video courses, books, articles, and interactive lessons.

Koen van Gilst

Written by

JavaScript Developer | M.A. in Philosophy | https://github.com/vnglst

EdgeCoders

jsComplete’s Medium Publication — jsComplete is a FREE software educational library focused on JavaScript, Node, React & GraphQL. It has video courses, books, articles, and interactive lessons.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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