articles

Home / DeveloperSection / Articles / Generate PDF in Node.JS

Generate PDF in Node.JS

Anchal Kesharwani26512 22-Sep-2014

In this article, I’m explaining how to generate pdf document in node.js. This article is based on the pdfkit module. 

PDFKit is pdf document generation library in node.js that make creating complex, multi-page, printable documents easy. It is written in pure CoffeeScript, but you can choose to use the API in plain ‘ol JavaScript if you like. 

In this article create only the simple pdf document by the help of express project and PDFKit module.

There are following steps to create generate pdf in node.js:

Step 1 Create an express project
 

It is not necessary to create but  create a simple demo that which understand the example. So create an express project by express.

Generate PDF in Node.JS

Now install all dependency by the npm install command. 

Step 2 Install PDFKit
 

How to install PDFKit for pdf generation it is necessary. Here I simply install by the npm install pdfkit command in command prompt:

 

Generate PDF in Node.JS

 

This install the pdfkit api and now need to use it. 

Step 3- Create an User Interface 

Here, create a simple form that get the file name and text content for pdf generation. Let’s create an form in /view/index.jade file:

extends layout  
block content
  h1= title
  p Welcome to #{title}
  form(method="post", action="/pdfGenrator")
   label(class="label-text") File Name
   br
   input(type="text", name="filename", placeholder="File Name", class="text-style", maxlength)
   br
   label(class="label-text") Text
   br
   textarea(name="content", class="text-style", placeholder="Write something for convert it to pdf")
   br
   input(type="submit", value="Create PDF", class="button-style") 


 This create a simple form as like this:

Generate PDF in Node.JS

 

In this form form action give as pdfGenrator and method is post. This form get the simply file and text content and create the pdf that text on that file name.

 

Step 5 Create PDF File

 

This section must important. In this section create a route inn index.js file in the route folder this works when form is submitted. First create the in /routes/index.js. Now add route /pdfGenerator:

router.post('/pdfGenrator',function(req,res){
                  var PDFDocument = require('pdfkit'); // add pdfkit module to access it
    var doc = new PDFDocument(); // create instance of PDFDocument     var path=require('path'); // add path module to get path     var filename=req.body.filename; // this file name is get by form     var content=req.body.content; // this text content is get by form     doc.y = 320; // this set the document horizontal position     doc.text(content,100,100);     doc.write(path.resolve(".")+'/PDF/'+filename+'.pdf'); // it create a file that write the document     res.download(path.resolve(".")+'/PDF/'+filename+'.pdf'); // it download this file     doc.end(); // document end by the end method });

 

This section create a pdf document in the base of form. And download that file that file after saving it. 

Note: Before run you should create a PDF folder in the application otherwise it give error. 

Step 6 Run Application 


Let’s ready to run it:

 

Generate PDF in Node.JS

 

Let’s check it:

 

Generate PDF in Node.JS

 

Let’s see that when click on the Create PDF button then it automatically download it:

 

Generate PDF in Node.JS

 

Let’s open this file and check it:

Generate PDF in Node.JS

This is successfully create a pdf file.

I hope that this article is helpful for you. Thanks!

 


Updated 03-Feb-2020

Leave Comment

Comments

Liked By