How to Create PDFs in Node.js Using PDFKit

PDFKit is a Node.js library that makes it easy for developers to create and work with PDF files. It offers a straightforward yet effective API for producing PDFs and adding different kinds of content to them, such as text, images, and shapes.

Sign up forfree

Forgot your password?

Create an account

*Required: 8 chars, 1 capital letter, 1 number

By continuing, you agree to thePrivacy PolicyandTerms of Use.You also agree to receive our newsletters, you can opt-out any time.

4

Setting Up PDFkit

You must haveNode.js and npm (the Node.js package manager) installed on your machineto follow along.

Ensure that you have these tools installed by running the following terminal commands:

iPhone in hand showing keyboard and paste option in notes app

To start using PDFKit in your Node.js project, install it by running the following npm command:

This command will install PDFKit and add it to your project’s dependencies.

YouTube Music Subscribe Page With US Five-Dollar Bill Underneath

Creating a PDF Document With PDFKit

To create a PDF document using PDFKit, require thepdfkitpackage and thefs(file system) module in your script file:

Next, create a new instance of thePDFDocumentclass. This class represents a PDF file:

A smart TV in a living room with a muted Mic appended on top

The instance created is a readable stream, which means it’s possible to use thepipe()method to save its contents to a file.

To do this, pipe thedocinstance into a writeable stream created byfs.createWriteStream:

Hand typing on a laptop with a document open on screen

ThecreateWriteStreammethod saves the contents of a PDF file to a specified location on the local file system.

The above code block will save the resulting PDF file to your current working directory, with the nameMyPDFDoc.pdf.

It is important to always end the stream after adding content to the PDF. You can do that using this command:

Now run the node commandnode [script name]to create the PDF fileMyPDFDoc.pdfin your current working directory.

Adding Text With PDFKit

Before you pipe your document to save it, you may make various changes. PDFKit makes adding text to documents simple and includes many options to customize the display of the output.

To add text to the document, simply call thetext()method and pass the text that you want to add as the argument:

As PDFKIT adds text to the document, it keeps track of that text’s current X and Y positions. So, whenever you call the text method, PDFKit will add a new line beneath the previous one.

However, by providing X and Y coordinates to the text method after the text itself, you can change its position:

To move up or down a line, call themoveDownormoveUpmethod with the number of lines the text should move:

By default, calling either method without passing an argument will cause it to move by just a single line.

PDFKit supports each of the 14 standard fonts defined by the PDF format. ThePDFKit text documentationhas a complete list of these standard fonts.

Use thefont()method to specify your required font. Just pass in the corresponding font name as a string:

you’re able to also change the font size and text color using thefontSize()and thefillColor()methods, respectively.

For example:

Adding Images With PDFKit

To an image to your PDF document, simply pass its path to theimagemethod on the document instance:

PDFKit allows you to add images in eitherJPEG or PNG format.

By default, PDFKit will insert the image at its native size. You can change an image’s dimensions by providing an object containing key-value pairs as an argument to theimage()method. You can specify a width, height, and horizontal and vertical alignment.

Adding Pages With PDFKit

To add new pages to the PDF document, simply call theaddPagemethod on the document instance:

To automatically add content every time you add a new page, use thepageAddedevent on the document instance.

PDFKit lets you specify the page dimensions when making a new document or adding a new page to an existing one. There are several predefined page sizes that thePDFKit paper documentationexplains.

To use any of these predefined page sizes, pass an object to either thePDFDocumentconstructor or theaddPage()method. Use the size property to specify a string which is the name of the paper size.

Passing this options object to thePDFDocumentconstructor sets the default page size for the whole document. you may then overwrite this default by passing a different value to theaddPagemethod.

You can set page margins by passing an object to themarginsproperty when adding pages.

Themarginsproperty takes an object withtop, bottom, left,andrightproperties representing each margin.

This code sets the top and bottom margins to 72 points, and the left and right margins to 50 points. Note that the default margin for PDFKit-created documents is a 1-inch (72-point) margin on all sides.

To set all the top, bottom, left, and right margins to the same value, simply pass an object with amarginproperty to theaddPagemethod:

The Advantages of Creating PDFs With PDFKit in Node.js

PDFKit enables you to produce standard and supported documents from your Node.js applications. With PDFKit, you can handle complex documents easily. Additionally, it can facilitate the delivery of documents to users and make them simpler to share and print.

Want to create professional-looking business reports or academic papers? Use these tips for formatting your Word documents.

Sometimes the smallest cleaning habit makes the biggest mess.

When your rival has to bail out your assistant.

Not all true crime is about hacking, slashing, and gore.

These plugins will make you wonder why you used Photoshop in the first place.

I gripped my chair the entire time—and then kept thinking about it when the screen turned off.

Technology Explained

PC & Mobile