1. Products
  2.   PDF
  3.   JavaScript
  4.   PDFKit
 
  

Create & Manage PDF Files via Free JavaScript API

Open Source Pure JavaScript Library allows to create PDF files, Insert images, text, set margin & add attachments to PDF for Node and web browser.

PDFKit is a very useful open source JavaScript library for creating and managing PDF documents with minimal effort and cost. The API is straightforward to handle and supports low-level functions as well as abstractions for higher-level functionality.

The PDFKit library is very feature-rich and has included support for numerous important features related to PDF documents creation and management such as inserting JPEG and PNG images, PDF encryption support, access privileges, inserting links, line wrapping, text alignment, adding bullet lists, text highlighting, font embedding, transformations support, adding Linear and radial gradients, adding Notes and many more.

The library is very stable and can be easily used on the client as well as server side. It is runnable in a browser as well as in Node.js and fully supports the creation of complex, multi-page, printable documents. It can be run in numerous popular browsers such as Internet Explorer, Firefox, Chrome, Opera, Safari, and more.

Previous Next

Getting Started with PDFKit

PDFKit is available at npm, You can easily download it and install it on your machine. Please use the following command for smooth installation.

Install PDFKit using npm

npm install pdfkit 

Create & Edit PDF Files using JavaScript Library

The Free PDFKit library can be used to create PDF documents inside your won JavaScript applications with ease. It makes JavaScript developer’s job easy by helping them to generate a custom PDF with complex, multi-page documents with just a couple of lines of code. It has included support for several important features, such as selecting page size, change default margin, selecting fonts types or font size, apply formatting and styles, and many more.

Create PDF File via JavaScript Library

const PDFDocument = require('pdfkit');
const fs = require('fs');

// Create a document
const doc = new PDFDocument();

// Pipe its output somewhere, like to a file or HTTP response
// See below for browser usage
doc.pipe(fs.createWriteStream('output.pdf'));

// Embed a font, set the font size, and render some text
doc
  .font('fonts/PalatinoBold.ttf')
  .fontSize(25)
  .text('Some text with an embedded font!', 100, 100);

// Add an image, constrain it to a given size, and center it vertically and horizontally
doc.image('path/to/image.png', {
  fit: [250, 300],
  align: 'center',
  valign: 'center'
});

// Add another page
doc
  .addPage()
  .fontSize(25)
  .text('Here is some vector graphics...', 100, 100);

// Finalize PDF file
doc.end();
   

Add & Manage Images inside PDF via JavaScript API

It is very easy to insert images inside a PDF document using the open source PDFKit library. You just need to provide the image path, buffer, or data uri with base64 encoded data and pass it to the image method. It has provided several features for handling images, such as customize image width and height, scale image, fit image inside a shape, stretch image, access image using URL, horizontally align the image, vertically align the image and so on.

How to Manage Images in PDF via JavaScript

 // Scale proprotionally to the specified width
doc.image('images/test.jpeg', 0, 15, {width: 300})
   .text('Proportional to width', 0, 0);

// Fit the image within the dimensions
doc.image('images/test.jpeg', 320, 15, {fit: [100, 100]})
   .rect(320, 15, 100, 100)
   .stroke()
   .text('Fit', 320, 0);

// Stretch the image
doc.image('images/test.jpeg', 320, 145, {width: 200, height: 100})
   .text('Stretch', 320, 130);

// Scale the image
doc.image('images/test.jpeg', 320, 280, {scale: 0.25})
   .text('Scale', 320, 265);

// Fit the image in the dimensions, and center it both horizontally and vertically
doc.image('images/test.jpeg', 430, 15, {fit: [100, 100], align: 'center', valign: 'center'})
   .rect(430, 15, 100, 100).stroke()
   .text('Centered', 430, 0);

Insert & Manage Text inside PDF via JS API

The open source JavaScript library PDFKit allows software professionals to insert as well as modify text inside their PDF documents. Text addition and customization is very easy to handle and the PDFKit library has provided several useful features for handling the text formation and styling. It has included features like text justification, line wrapping, line break, text indentations, set space between paragraphs, stroke text, lists addition, rich text support and many more.

How to Apply Text Justification via JavaScript API

 const lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus.  Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit. Ut nec accumsan nisl.';

doc.fontSize(8);
doc.text(`This text is left aligned. ${lorem}`, {
  width: 410,
  align: 'left'
}
);

doc.moveDown();
doc.text(`This text is centered. ${lorem}`, {
  width: 410,
  align: 'center'
}
);

doc.moveDown();
doc.text(`This text is right aligned. ${lorem}`, {
  width: 410,
  align: 'right'
}
);

doc.moveDown();
doc.text(`This text is justified. ${lorem}`, {
  width: 410,
  align: 'justify'
}
);

// draw bounding rectangle
doc.rect(doc.x, 0, 410, doc.y).stroke();
 English