parallax/jsPDF · error · Error

The option pdfjsnewwindow just works in a browser-environmen

Error message

The option pdfjsnewwindow just works in a browser-environment.

What it means

Thrown by jsPDF.output('pdfjsnewwindow') when globalObject is not a browser Window. The pdf.js viewer target needs window.open to create a new window and inject the PDF.js iframe, which is unavailable outside a browser, so the call is rejected.

Source

Thrown at src/jspdf.js:3208

            pdfViewer.height = "400px";
            pdfViewer.src =
              pdfJsUrl +
              pdfJsQueryChar +
              "file=&downloadName=" +
              encodeURIComponent(options.filename);

            pdfViewer.onload = function() {
              PDFjsNewWindow.document.title = options.filename;
              pdfViewer.contentWindow.PDFViewerApplication.open(
                scope.output("bloburl")
              );
            };

            initializedPdfJsWindow.body.appendChild(pdfViewer);
          }
          return PDFjsNewWindow;
        } else {
          throw new Error(
            "The option pdfjsnewwindow just works in a browser-environment."
          );
        }
      case "dataurlnewwindow":
        if (
          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);

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Switch to a Node-safe output in non-browser environments: `doc.output('arraybuffer')` or `doc.output('datauristring')`.
  2. Branch on `typeof window !== 'undefined'` before choosing the 'pdfjsnewwindow' target.
  3. Serve the generated PDF bytes through your own HTTP endpoint and let a browser load PDF.js against that URL.

Example fix

// before
doc.output('pdfjsnewwindow', { pdfJsUrl: '/viewer.html' }); // in Node
// after
const isBrowser = typeof window !== 'undefined';
isBrowser
  ? doc.output('pdfjsnewwindow', { pdfJsUrl: '/viewer.html' })
  : require('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('pdfjsnewwindow', { pdfJsUrl: '/viewer.html' });
} else {
  require('fs').writeFileSync('out.pdf', Buffer.from(doc.output('arraybuffer')));
}

Type guard

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

Try / catch

try { doc.output('pdfjsnewwindow', { pdfJsUrl }); }
catch (e) { return doc.output('arraybuffer'); }

Prevention

When it happens

Trigger: Calling `doc.output('pdfjsnewwindow', { pdfJsUrl })` in Node.js, a worker, or any non-browser host. The `[object Window]` check fails and the else branch throws.

Common situations: Server-side generation reused from browser code; Node-based PDF services; test environments without a DOM; Electron main process.

Related errors


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