CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pdfkit

A PDF generation library for Node.js with comprehensive text, graphics, and form support

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

index.mddocs/

PDFKit

PDFKit is a comprehensive PDF generation library for Node.js and browsers that enables developers to create complex, multi-page, printable documents programmatically. It offers both low-level PDF operations and high-level abstractions for vector graphics, advanced text handling, font embedding, image integration, interactive forms, and accessibility features.

Package Information

  • Package Name: pdfkit
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install pdfkit

Core Imports

import PDFDocument from "pdfkit";

For CommonJS:

const PDFDocument = require("pdfkit");

For browsers:

<script src="path/to/pdfkit.standalone.js"></script>
<!-- PDFDocument is available globally -->

Basic Usage

import PDFDocument from "pdfkit";
import fs from "fs";

// Create a new PDF document
const doc = new PDFDocument({
  size: 'A4',
  margins: { top: 50, left: 50, right: 50, bottom: 50 }
});

// Pipe the PDF to a file
doc.pipe(fs.createWriteStream('output.pdf'));

// Add content
doc.fontSize(20)
   .fillColor('blue')
   .text('Hello PDFKit!', 100, 100);

doc.rect(100, 150, 200, 100)
   .fillColor('red')
   .fill();

// Finalize the PDF
doc.end();

Architecture

PDFKit is built around several key components:

  • PDFDocument Class: Main class extending Node.js Readable stream for PDF generation
  • Mixin Architecture: Functionality organized into specialized mixins (Color, Vector, Text, Images, etc.)
  • Page Management: Automatic and manual page handling with buffering support
  • Stream-Based Output: Compatible with Node.js streams for flexible output handling
  • Cross-Platform: Works in Node.js and modern browsers with different build targets

Capabilities

Document Management

Core document creation, page management, and output control functionality.

class PDFDocument extends stream.Readable {
  constructor(options?: PDFDocumentOptions);
  addPage(options?: PageOptions): PDFDocument;
  end(): void;
}

interface PDFDocumentOptions {
  pdfVersion?: '1.3' | '1.4' | '1.5' | '1.6' | '1.7' | '1.7ext3';
  compress?: boolean;
  autoFirstPage?: boolean;
  size?: string | [number, number];
  layout?: 'portrait' | 'landscape';
  margins?: MarginDefinition;
  bufferPages?: boolean;
  font?: string | Buffer | ArrayBuffer;
  fontSize?: number;
  info?: DocumentInfo;
  displayTitle?: boolean;
  lang?: string;
  ignoreOrientation?: boolean;
  subset?: PDFSubset;
  tagged?: boolean;
}

Document Management

Text Rendering

Advanced text rendering with flexible positioning, styling, fonts, and layout options including line wrapping and multi-column support.

text(text: string, x?: number, y?: number, options?: TextOptions): PDFDocument;
widthOfString(string: string, options?: TextOptions): number;
font(src: string | Buffer, family?: string, size?: number): PDFDocument;
fontSize(size: SizeValue): PDFDocument;

Text Rendering

Vector Graphics

Comprehensive vector graphics with path construction, shapes, transformations, and painting operations using HTML5 canvas-like API.

rect(x: number, y: number, w: number, h: number): PDFDocument;
circle(x: number, y: number, radius: number): PDFDocument;
moveTo(x: number, y: number): PDFDocument;
lineTo(x: number, y: number): PDFDocument;
fill(color?: ColorValue, rule?: FillRule): PDFDocument;
stroke(color?: ColorValue): PDFDocument;

Vector Graphics

Color Management

Color handling with support for RGB, CMYK, hex colors, gradients, patterns, and spot colors.

fillColor(color: ColorValue, opacity?: number): PDFDocument;
strokeColor(color: ColorValue, opacity?: number): PDFDocument;
linearGradient(x1: number, y1: number, x2: number, y2: number): Gradient;
radialGradient(x1: number, y1: number, r1: number, x2: number, y2: number, r2: number): Gradient;

Color Management

Image Handling

Image embedding and rendering with support for JPEG and PNG formats, including transparent PNGs and EXIF orientation.

image(src: ImageSource, x?: number, y?: number, options?: ImageOptions): PDFDocument;
openImage(src: ImageSource): PDFImage;

type ImageSource = string | Buffer | ArrayBuffer;

Image Handling

Interactive Elements

Annotations, hyperlinks, form fields, and interactive PDF features for creating engaging documents.

link(x: number, y: number, w: number, h: number, url: string, options?: AnnotationOptions): PDFDocument;
formText(name: string, x: number, y: number, w: number, h: number, options?: FormFieldOptions): PDFDocument;
note(x: number, y: number, w: number, h: number, contents: string, options?: AnnotationOptions): PDFDocument;

Interactive Elements

Font Management

Advanced font handling with font registration, size utilities, and typography controls.

font(src: string | Buffer | ArrayBuffer, family?: string, size?: number): PDFDocument;
fontSize(size: SizeValue): PDFDocument;
registerFont(name: string, src: string | Buffer | ArrayBuffer, family?: string): PDFDocument;
currentLineHeight(includeGap?: boolean): number;

Font Management

Document Outline

PDF bookmarks and navigation structure for creating organized document outlines.

outline.addItem(title: string, options?: OutlineOptions): OutlineItem;

interface OutlineOptions {
  expanded?: boolean;
  destination?: string;
}

Document Outline

File Attachments

Embed files within PDF documents for comprehensive document packages.

file(src: string | Buffer, options?: FileOptions): PDFReference;

interface FileOptions {
  name?: string;
  type?: string;
  description?: string;
  hidden?: boolean;
  creationDate?: Date;
  modifiedDate?: Date;
}

File Attachments

Tables

Structured table creation with comprehensive layout and styling options.

table(options?: TableOptions): PDFTable;

interface PDFTable {
  row(data: any[], lastRow?: boolean): PDFTable | PDFDocument;
  end(): PDFDocument;
}

Tables

Accessibility Features

Tagged PDF support, structure elements, and accessibility features for creating inclusive documents.

struct(type: StructureType, options?: StructureOptions, children?: StructureElement[]): StructureElement;
markContent(tag: string, options?: MarkingOptions): PDFDocument;

Accessibility Features

Types

type ColorValue = string | [number, number, number] | [number, number, number, number] | Gradient | Pattern;
type SizeValue = number | string;
type MarginDefinition = number | [number, number] | [number, number, number, number] | 
  { vertical: number; horizontal: number } | 
  { top: number; right: number; bottom: number; left: number };

interface DocumentInfo {
  Title?: string;
  Author?: string;
  Subject?: string;
  Keywords?: string;
  Creator?: string;
  Producer?: string;
  CreationDate?: Date;
  ModDate?: Date;
}

type PDFSubset = 'PDF/A-1' | 'PDF/A-1a' | 'PDF/A-1b' | 'PDF/A-2' | 'PDF/A-2a' | 'PDF/A-2b' | 'PDF/A-3' | 'PDF/A-3a' | 'PDF/A-3b' | 'PDF/UA';

// Standard page sizes supported
type PageSize = 'A0' | 'A1' | 'A2' | 'A3' | 'A4' | 'A5' | 'A6' | 'A7' | 'A8' | 'A9' | 'A10' |
  'B0' | 'B1' | 'B2' | 'B3' | 'B4' | 'B5' | 'B6' | 'B7' | 'B8' | 'B9' | 'B10' |
  'C0' | 'C1' | 'C2' | 'C3' | 'C4' | 'C5' | 'C6' | 'C7' | 'C8' | 'C9' | 'C10' |
  'RA0' | 'RA1' | 'RA2' | 'RA3' | 'RA4' | 'SRA0' | 'SRA1' | 'SRA2' | 'SRA3' | 'SRA4' |
  'EXECUTIVE' | 'FOLIO' | 'LEGAL' | 'LETTER' | 'TABLOID';

Install with Tessl CLI

npx tessl i tessl/npm-pdfkit

docs

accessibility-features.md

attachments.md

color-management.md

document-management.md

font-management.md

image-handling.md

index.md

interactive-elements.md

outline.md

tables.md

text-rendering.md

vector-graphics.md

tile.json