parallax/jsPDF · error · Error

Font does not exist in vFS, import fonts or remove declarati

Error message

Font does not exist in vFS, import fonts or remove declaration doc.addFont('${font.postScriptName}').

What it means

This is the else branch of the same ttfsupport 'addFont' handler, reached only when typeof data.instance === 'undefined' for a non-standard font — i.e. the addFont event fired without a jsPDF instance attached to the event data. Without an instance there is no VFS to consult, so the handler cannot resolve the font and throws, advising you to import the font or remove the declaration. In normal use the instance is always bound, so hitting this usually means the font API was invoked outside a fully-constructed jsPDF instance or via a manually dispatched internal event.

Source

Thrown at src/modules/ttfsupport.js:66

      if (font.isStandardFont) {
        return;
      }
      if (typeof instance !== "undefined") {
        if (instance.existsFileInVFS(font.postScriptName) === false) {
          file = instance.loadFile(font.postScriptName);
        } else {
          file = instance.getFileFromVFS(font.postScriptName);
        }
        if (typeof file !== "string") {
          throw new Error(
            "Font is not stored as string-data in vFS, import fonts or remove declaration doc.addFont('" +
              font.postScriptName +
              "')."
          );
        }
        addFont(font, file);
      } else {
        throw new Error(
          "Font does not exist in vFS, import fonts or remove declaration doc.addFont('" +
            font.postScriptName +
            "')."
        );
      }
    }
  ]); // end of adding event handler
})(jsPDF);

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Perform all font registration on a real, constructed instance: var doc = new jsPDF(); doc.addFileToVFS(...); doc.addFont(...);
  2. Make sure font setup runs after the jsPDF instance exists, not at import/eval time.
  3. If you are firing the 'addFont' event manually, include instance in the event data payload.

Example fix

// before
// firing addFont internals with no instance, or registering fonts before new jsPDF()

// after
var doc = new jsPDF();
doc.addFileToVFS('MyFont.ttf', base64String);
doc.addFont('MyFont.ttf', 'MyFont', 'normal');
Defensive patterns

Strategy: validation

Validate before calling

function safeAddFont(maybeDoc, postScriptName, id, style) {
  if (!maybeDoc || typeof maybeDoc.addFileToVFS !== 'function') {
    throw new TypeError('Cannot register font: no jsPDF instance available');
  }
  if (typeof maybeDoc.getFileFromVFS(postScriptName) !== 'string') {
    throw new Error('Font ' + postScriptName + ' missing from VFS on this instance');
  }
  maybeDoc.addFont(postScriptName, id, style);
}

Type guard

const isJsPdfInstance = (d) => !!d && typeof d === 'object' && typeof d.addFont === 'function' && typeof d.addFileToVFS === 'function';
// usage: if (!isJsPdfInstance(doc)) { /* construct new jsPDF() first */ }

Try / catch

try {
  doc.addFont('MyFont.ttf', 'MyFont', 'normal');
} catch (e) {
  if (/Font does not exist in vFS/.test(e.message)) {
    console.error('addFont fired without a usable instance/VFS — ensure doc = new jsPDF() first:', e.message);
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: Manually dispatching/firing the 'addFont' event without supplying instance in the data payload; calling font-registration hooks before 'new jsPDF()' has returned; operating on a stripped/custom build where the instance is not propagated into event data; running font setup in an SSR/Node context where the global jsPDF differs from the document's instance.

Common situations: Using a custom or forked jsPDF build that drops the instance binding; invoking internal jsPDF.API event handlers directly in tests; registering fonts at module-eval time before any doc is constructed; mixing multiple jsPDF instances/versions in one bundle.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/656c4d4ee499bece. Report an issue: GitHub.