Skip to content

Support embedded-only fonts - avoid loading defaults during initialization #1647

Open

Description

@antaranyan

Bug Report

Description of the problem

PDFKit currently requires filesystem access to load default fonts during document initialization, even when later using only embedded fonts via registerFont(). This breaks in environments where system font paths are unavailable, e.g. compiled portable Deno executable in my case.

Code sample

// Fails - tries to load system fonts during initialization
const doc1 = new PDFDocument();

// Also fails - undefined still triggers default font loading
const doc2 = new PDFDocument({ font: undefined });

My current workaround is to initialize document with empty string to bypass default font loading, then register embedded fonts:

const doc = new PDFDocument({ font: "" }); // Skip system font loading
doc.registerFont('Courier', embeddedCourierData);
doc.font('Courier');

But would appreciate a cleaner solution.

Your environment

  • pdfkit version: 0.17.0
  • Node Deno version: 2.5.0
  • Browser version (if applicable): -
  • Operating System: Arch Linux

Activity

  1. monolithed commented on Dec 5, 2025

    @monolithed

    Unfortunately, this workaround breaks something else. If no text is provided, almost any call ends up throwing an error.

       foo(text: string)  {
            if (!text) {
                this.document.moveDown(1);
    
                return void 0;
            }
    
            this.document
                .font(Fonts.NOTO_SANS_BOLD)
                .text(text, 0, this.document.y, {
                    align: Position.RIGHT
                });
        }

    In the example with .moveDown, it will fail at:

     currentLineHeight(includeGap) {
        return this._font.lineHeight(this._fontSize, includeGap);
            ^ caught (in promise) TypeError: Cannot read properties of null (reading 'lineHeight')
      },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions