parallax/jsPDF · warning · Error

Invalid Context passed to initialize PubSub (jsPDF-module)

Error message

Invalid Context passed to initialize PubSub (jsPDF-module)

What it means

The internal PubSub event bus rejects construction unless it is handed an object as its context. The guard `typeof context !== "object"` at src/jspdf.js:22 is a fail-fast invariant: PubSub needs a context to scope event handlers into. In production code the only call site is `new PubSub(API)` at src/jspdf.js:1063, where API is always an object literal, so a normal user almost never triggers this. It surfaces when code reaches into `jsPDF.API.__private__.PubSub` and instantiates it manually, or when the API object has been replaced/corrupted by a bundler, a bad plugin, or a monkey-patch that reassigned the internal API to a primitive.

Source

Thrown at src/jspdf.js:23

// @endif
import { globalObject } from "./libs/globalObject.js";
import { RGBColor } from "./libs/rgbcolor.js";
import { btoa } from "./libs/AtobBtoa.js";
import { console } from "./libs/console.js";
import { PDFSecurity } from "./libs/pdfsecurity.js";
import { toPDFName } from "./libs/pdfname.js";
/**
 * jsPDF's Internal PubSub Implementation.
 * Backward compatible rewritten on 2014 by
 * Diego Casorran, https://github.com/diegocr
 *
 * @class
 * @name PubSub
 * @ignore
 */
function PubSub(context) {
  if (typeof context !== "object") {
    throw new Error(
      "Invalid Context passed to initialize PubSub (jsPDF-module)"
    );
  }
  var topics = {};

  this.subscribe = function(topic, callback, once) {
    once = once || false;
    if (
      typeof topic !== "string" ||
      typeof callback !== "function" ||
      typeof once !== "boolean"
    ) {
      throw new Error(
        "Invalid arguments passed to PubSub.subscribe (jsPDF-module)"
      );
    }

    if (!topics.hasOwnProperty(topic)) {

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Do not construct PubSub yourself — it is an internal class created once per jsPDF instance; rely on the instance's own event API instead.
  2. If you are patching internals, verify `typeof API === "object" && API !== null` before `new PubSub(API)` runs; `typeof null === "object"` so also guard against null.
  3. On a version upgrade, diff the internal API surface (`API.__private__`) — if PubSub moved, update your plugin to use the new event hooks (`doc.internal.events`) rather than reconstructing the bus.
  4. If the error appears without custom code, clear bundler caches (node_modules, webpack/vite cache) and rebuild — a corrupted internal object is the likely cause.

Example fix

// before (broken: passing a primitive)
var bus = new jsPDF.API.__private__.PubSub('myEvents');

// after: reuse the instance's own event bus
doc.internal.events.subscribe('addPage', function () { /* ... */ });
Defensive patterns

Strategy: type-guard

Validate before calling

function isPubSubContext(ctx) {
  return typeof ctx === 'object' && ctx !== null && typeof ctx.subscribe !== 'function' || ctx === undefined;
}
// only instantiate if valid; otherwise reuse the instance's own bus
if (isPubSubContext(myApi)) {
  new jsPDF.API.__private__.PubSub(myApi);
}

Type guard

function isPubSubContext(ctx) {
  return typeof ctx === 'object' && ctx !== null;
}

Try / catch

try {
  var bus = new jsPDF.API.__private__.PubSub(ctx);
} catch (e) {
  if (/Invalid Context/.test(e.message)) {
    // fall back to the document's existing event bus
    bus = doc.internal.events;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `new jsPDF.API.__private__.PubSub("foo")` / `new PubSub(42)` / `new PubSub(undefined)` directly; a plugin or bundler that reassigns the `API` object to a non-object before `new PubSub(API)` runs at src/jspdf.js:1063; a tree-shaking/optimization bug that drops the API object initialization leaving a primitive in its place.

Common situations: Custom plugins that manually re-instantiate the event bus; broken bundler output after a major jsPDF version upgrade where module internals were reorganized; test harnesses that stub out globals and accidentally leak a primitive into the constructor path; monkey-patching jsPDF internals (e.g. to intercept events) without preserving object identity.

Related errors


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