Click here to Skip to main content
13,835,897 members
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 21 Apr 2014
Licenced MIT

Generating Server Side PDFs using PDFKit and Node.js

, 21 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Generating server side PDFs in 5 minutes with PDFKit and Node.js

Nodejs PDF PDFkit


Generating PDFs looks like a massive task when it comes to you at first, but with PDFKit it becomes a cake walk.

What's surprising is that not many people know about PDFKit module for Node.js. It makes dealing with PDF documents extremely easy. The module takes out all the complexities involved and provides simple APIs written in CoffeeScript which can also be used as plain JavaScript. In this post, we will be working towards generating a simple PDF document with text content on server side using PDFKit module and Node.js. So let's get started.

First of all, as we all know, we need to install the module through npm:

npm install pdfkit

Next, we make a generatePDFDocument.js file and write the following code in it:

var PDF = require('pdfkit');            //including the pdfkit module
var fs = require('fs');

doc = new PDF();                        //creating a new PDF object
doc.pipe(fs.createWriteStream('PATH_TO_PDF_FILE'));  //creating a write stream 
            //to write the content on the file system
doc.text(text, 100, 100);             //adding the text to be written, 
            // more things can be added here including new pages
doc.end(); //we end the document writing.

That's all the code we need to create a simple PDF document with text content. Now, we just need to run this using node as follows:

node PATH_TO/generatePDFDocument.js

And it should create a new PDF document at the path you provided in the code to the writeStream. That's it folks, it took us only about 5 minutes to generate a PDF on server.

And There is a Lot More

There is a lot more that you can do with PDFKit, including Vector Graphics, Rich Text, Images, Annotations, etc.

For more information on the APIs and other examples, please refer to github.

We will be covering more about PDFs and use of PhantomJS (another powerful library) with Node.js in the coming posts.


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Suroor Wijdan
Software Developer (Senior)
India India
Suroor speaks JavaScript and has good social media influence, great passion to write technical blogs which lead to the birth of community blog Loves to hack bleeding edge technologies and contributes to open source on regular basis. He is the part of core team behind NodeConsole, has written various npm modules and utilities for Node.js. Also he likes to watch House of Cards in free time which is rare!

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190114.1 | Last Updated 22 Apr 2014
Article Copyright 2014 by Suroor Wijdan
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid