parallax/jsPDF · error · Error

The option dataurlnewwindow just works in a browser-environm

Error message

The option dataurlnewwindow just works in a browser-environment.

What it means

Thrown by jsPDF.output('dataurlnewwindow') when globalObject is not a browser Window. The target opens a new window and points an iframe at a data URL; without a real window object this is impossible, so the call aborts.

Source

Thrown at src/jspdf.js:3232

          Object.prototype.toString.call(globalObject) === "[object Window]"
        ) {
          var dataURLNewWindow = globalObject.open();
          if (dataURLNewWindow !== null) {
            var initializedDataUrlWindow = initializeNewWindow(
              dataURLNewWindow
            );
            var dataUrlFrame = initializedDataUrlWindow.document.createElement(
              "iframe"
            );

            dataUrlFrame.src = this.output("datauristring", options);
            initializedDataUrlWindow.body.appendChild(dataUrlFrame);
            dataURLNewWindow.document.title = options.filename;
          }
          if (dataURLNewWindow || typeof safari === "undefined")
            return dataURLNewWindow;
        } else {
          throw new Error(
            "The option dataurlnewwindow just works in a browser-environment."
          );
        }
        break;
      case "datauri":
      case "dataurl":
        return (globalObject.document.location.href = this.output(
          "datauristring",
          options
        ));
      default:
        return null;
    }
  }));

  /**
   * Used to see if a supplied hotfix was requested when the pdf instance was created.
   * @param {string} hotfixName - The name of the hotfix to check.

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Use a Node-compatible target in Node: `doc.output('arraybuffer')`, `doc.output('datauristring')`, or `doc.output('blob')` with a polyfill.
  2. Guard the call: only use 'dataurlnewwindow' when `typeof window !== 'undefined'`.
  3. Return the data URL string to the client and open it there instead of doing it server-side.

Example fix

// before
doc.output('dataurlnewwindow'); // runs in Node
// after
if (typeof window !== 'undefined') {
  doc.output('dataurlnewwindow');
} else {
  const fs = require('fs');
  fs.writeFileSync('out.pdf', Buffer.from(doc.output('arraybuffer')));
}
Defensive patterns

Strategy: validation

Validate before calling

const isBrowser = typeof window !== 'undefined' && Object.prototype.toString.call(window) === '[object Window]';
if (isBrowser) {
  doc.output('dataurlnewwindow', options);
} else {
  const dataUrl = doc.output('datauristring');
  // store/return dataUrl for the client to open
}

Type guard

function isBrowserWindow(g) { return Object.prototype.toString.call(g) === '[object Window]'; }

Try / catch

try { doc.output('dataurlnewwindow', options); }
catch (e) { return doc.output('datauristring'); }

Prevention

When it happens

Trigger: Calling `doc.output('dataurlnewwindow', options)` in Node.js, a worker, SSR, or jsdom lacking a true window. The `[object Window]` check fails and the else branch throws.

Common situations: SSR pipelines sharing code with the browser; Node scripts generating PDFs; CI/test runs in Node; headless browser shells without a window global.

Related errors


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