Click here to Skip to main content
Click here to Skip to main content

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

Introduction

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');
var text = 'ANY_TEXT_YOU_WANT_TO_WRITE_IN_PDF_DOC';

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.

License

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

Share

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 howtojs.org. 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!
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 22 Apr 2014
Article Copyright 2014 by Suroor Wijdan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid