{"record":{"id":"895816250d51404f","repo":"parallax/jsPDF","slug":"the-option-pdfobjectnewwindow-just-works-in-a-brow","errorCode":null,"errorMessage":"The option pdfobjectnewwindow just works in a browser-environment.","messagePattern":"The option pdfobjectnewwindow just works in a browser-environment\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":3169,"sourceCode":"            var scope = this;\n\n            pdfObjectScript.src = pdfObjectUrl;\n\n            if (useDefaultPdfObjectUrl) {\n              pdfObjectScript.integrity =\n                \"sha512-4ze/a9/4jqu+tX9dfOqJYSvyYd5M6qum/3HpCLr+/Jqf0whc37VUbkpNGHR7/8pSnCFw47T1fmIpwBV7UySh3g==\";\n              pdfObjectScript.crossOrigin = \"anonymous\";\n            }\n\n            pdfObjectScript.onload = function() {\n              nW.PDFObject.embed(scope.output(\"dataurlstring\"), options);\n            };\n\n            initializedPdfObjectWindow.body.appendChild(pdfObjectScript);\n          }\n          return nW;\n        } else {\n          throw new Error(\n            \"The option pdfobjectnewwindow just works in a browser-environment.\"\n          );\n        }\n      case \"pdfjsnewwindow\":\n        if (\n          Object.prototype.toString.call(globalObject) === \"[object Window]\"\n        ) {\n          var pdfJsUrl = options.pdfJsUrl || \"examples/PDF.js/web/viewer.html\";\n          var PDFjsNewWindow = globalObject.open();\n\n          if (PDFjsNewWindow !== null) {\n            var initializedPdfJsWindow = initializeNewWindow(PDFjsNewWindow);\n            var pdfViewer = initializedPdfJsWindow.document.createElement(\n              \"iframe\"\n            );\n            var pdfJsQueryChar = pdfJsUrl.indexOf(\"?\") === -1 ? \"?\" : \"&\";\n            var scope = this;\n","sourceCodeStart":3151,"sourceCodeEnd":3187,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L3151-L3187","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndoc.output('pdfobjectnewwindow'); // in Node\n// after\nif (typeof window !== 'undefined') {\n  doc.output('pdfobjectnewwindow');\n} else {\n  require('fs').writeFileSync('out.pdf', doc.output('arraybuffer'));\n}","handlingStrategy":"validation","validationCode":"const isBrowser = typeof window !== 'undefined' && Object.prototype.toString.call(window) === '[object Window]';\nif (isBrowser) {\n  doc.output('pdfobjectnewwindow', options);\n} else {\n  require('fs').writeFileSync('out.pdf', Buffer.from(doc.output('arraybuffer')));\n}","typeGuard":"function isBrowserWindow(g) { return Object.prototype.toString.call(g) === '[object Window]'; }","tryCatchPattern":"try { doc.output('pdfobjectnewwindow', options); }\ncatch (e) { /* fallback: write to file or return data URL */\n  console.warn('pdfobjectnewwindow unavailable; falling back to arraybuffer');\n  return doc.output('arraybuffer');\n}","preventionTips":["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."],"tags":["environment","browser","output","node","ssr"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}