Member-only story

NodeJS : How to generate and properly serve PDF

Nikhil Nanjappa
3 min readApr 28, 2018

--

When it comes to client side PDF generation, there are loads of guides out there especially when compared to the same implementation on server side & even lesser guides on how to serve that generated PDF data properly to the client(or store it in the server if need be). In this guide I will be concentrating on exactly those using NodeJS environment, namely:

  1. Generate a PDF and respond to client(base64 string) and how to use that string in the client-side to display the PDF.
  2. Generate a PDF and respond to client for auto download.
  3. Generate a PDF and store it in the server.

For this guide, I’m going to make use of the pdfmake npm package which is one of the best in business for PDF creation. The package can be installed by running the following command

npm install pdfmake --save

I will refrain from diving deep into the details of the package methods(which you can read from here), instead I will concentrate more on implementing the package to generate a PDF and more importantly how to handle that PDF data.

Generate a PDF and respond to client(base64 string)

Let’s consider a simple example to create PDF using pdfmake and responding with base64 string is:

--

--

Nikhil Nanjappa
Nikhil Nanjappa

Written by Nikhil Nanjappa

Experienced Full-Stack Developer who loves sharing knowledge. Gaming & Music addict. Always grateful and looking for new experiences in life.

Responses (6)