Create PDF using Node.js – PDFKit tutorial

In this article, we have covered how to create PDF using Node.js. When it comes to creating PDF documents using Node.js, PDFKit is a powerful library that developers can leverage to generate complex PDF files programmatically. PDFKit provides a high-level document construction API that allows for the creation of text, images, vector graphics, and more within a PDF.

Interactive Table of Contents

Key Features of PDFKit

PDFKit offers a variety of features to create PDF using Node.js:

  • Vector graphics support, including lines, curves, and shapes.
  • Advanced text layout and font embedding.
  • Image embedding with support for JPEG, PNG, and SVG.
  • PDF annotation, layer, and hyperlink capabilities.
  • Support for adding and merging PDF documents.

Main Functions of PDFKit

The key functionalities provided by PDFKit include:

  • PDFDocument: The starting point for creating a new PDF document.
  • addPage: Used to add new pages to the document.
  • font: Specify the font to be used in the document.
  • text: Add text content to the document at a specific location.
  • image: Place images within the document.
  • moveTo, lineTo, stroke: Draw lines and shapes.

PDFKit Examples

Below are some examples showcasing how to utilize PDFKit to create PDF documents in Node.js.

Creating a Simple PDF Document

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

    const doc = new PDFDocument();
    doc.pipe(fs.createWriteStream('output.pdf'));
    
    doc.fontSize(25)
       .text('Creating PDFs in Node.js with PDFKit', 100, 80);
    
    doc.end();

Adding Images and Graphics

    // Assuming 'imagePath' holds the path to an image file
    doc.image(imagePath, {
        fit: [250, 300],
        align: 'center',
        valign: 'center'
    });

    // Drawing a rectangle
    doc.rect(100, 150, 100, 200)
       .stroke();

Custom Font and Styles

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

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

// Pipe the PDF content to a file
const outputPath = 'example2.pdf';
const outputStream = fs.createWriteStream(outputPath);
doc.pipe(outputStream);

// Set font and font size
doc.font('Helvetica-Bold');
doc.fontSize(24);

// Add styled text to the PDF
doc.text('This is a custom font and style.', { align: 'center' });

// Finalize the PDF
doc.end();

console.log(`PDF created at: ${outputPath}`);

 

Draw Shapes

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

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

// Pipe the PDF content to a file
const outputPath = 'example3.pdf';
const outputStream = fs.createWriteStream(outputPath);
doc.pipe(outputStream);

// Draw shapes on the PDF
doc.rect(50, 50, 200, 100).fill('#3498db'); // Rectangle
doc.circle(350, 100, 50).fill('#e74c3c'); // Circle

// Finalize the PDF
doc.end();

console.log(`PDF created at: ${outputPath}`);

 

References

Conclusive Summary

PDFKit is an essential Node.js library for developers looking to create PDF web services or applications. Its features allow for both simple and complex PDF creation, with the ability to add text, vector graphics, and images. This versatility makes it an excellent choice for Node.js developers aiming to integrate PDF functionality into their applications. By harnessing the key functions and examples provided, one can create dynamic and rich PDF documents tailored to various needs.