parallax/jsPDF · error · Error
The option pdfobjectnewwindow just works in a browser-enviro
Error message
The option pdfobjectnewwindow just works in a browser-environment.
What it means
Thrown by jsPDF.output('pdfobjectnewwindow') when the runtime is not a browser. The case checks `Object.prototype.toString.call(globalObject) === '[object Window]'`; in Node.js or any non-window host globalObject is not a Window, so embedding via PDFObject in a new window is impossible and the call aborts.
Source
Thrown at src/jspdf.js:3169
var scope = this;
pdfObjectScript.src = pdfObjectUrl;
if (useDefaultPdfObjectUrl) {
pdfObjectScript.integrity =
"sha512-4ze/a9/4jqu+tX9dfOqJYSvyYd5M6qum/3HpCLr+/Jqf0whc37VUbkpNGHR7/8pSnCFw47T1fmIpwBV7UySh3g==";
pdfObjectScript.crossOrigin = "anonymous";
}
pdfObjectScript.onload = function() {
nW.PDFObject.embed(scope.output("dataurlstring"), options);
};
initializedPdfObjectWindow.body.appendChild(pdfObjectScript);
}
return nW;
} else {
throw new Error(
"The option pdfobjectnewwindow just works in a browser-environment."
);
}
case "pdfjsnewwindow":
if (
Object.prototype.toString.call(globalObject) === "[object Window]"
) {
var pdfJsUrl = options.pdfJsUrl || "examples/PDF.js/web/viewer.html";
var PDFjsNewWindow = globalObject.open();
if (PDFjsNewWindow !== null) {
var initializedPdfJsWindow = initializeNewWindow(PDFjsNewWindow);
var pdfViewer = initializedPdfJsWindow.document.createElement(
"iframe"
);
var pdfJsQueryChar = pdfJsUrl.indexOf("?") === -1 ? "?" : "&";
var scope = this;
View on GitHub (pinned to a3930ce03a)
Solutions
- In Node, use a Node-safe target: `doc.output('arraybuffer')`, `doc.output('blob')` (with a polyfill), or `doc.output('datauristring')` and write it yourself.
- Gate the call on environment: only invoke 'pdfobjectnewwindow' when `typeof window !== 'undefined'`.
- If you need the viewer in Node, render the PDF to a buffer and serve it via an HTTP response instead of opening a window.
Example fix
// before
doc.output('pdfobjectnewwindow'); // in Node
// after
if (typeof window !== 'undefined') {
doc.output('pdfobjectnewwindow');
} else {
require('fs').writeFileSync('out.pdf', 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('pdfobjectnewwindow', options);
} 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('pdfobjectnewwindow', options); }
catch (e) { /* fallback: write to file or return data URL */
console.warn('pdfobjectnewwindow unavailable; falling back to arraybuffer');
return doc.output('arraybuffer');
} Prevention
- Branch output target on environment in a shared helper.
- Never call window-dependent output targets in Node/SSR paths.
- Unit-test PDF generation in Node using arraybuffer/blob output.
When it happens
Trigger: Calling `doc.output('pdfobjectnewwindow', options)` in Node.js, a web worker, jsdom without a window, or during SSR. The pdfobjectnewwindow target requires a real browser window object to call window.open and inject the PDFObject script.
Common situations: Server-side rendering pipelines that reuse the same jsPDF call path as the browser; running PDF generation in an Electron main process or a Node script; test runners (jest/mocha) executing in Node.
Related errors
- The option dataurlnewwindow just works in a browser-environm
- The option pdfjsnewwindow just works in a browser-environmen
- Error in function {function_name}: {message}
- Trying to read a file from local file system. To enable this
- Cannot read file '${url}'. Permission denied.
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/895816250d51404f.
Report an issue: GitHub.