{"record":{"id":"e71ccc3b1584d6ee","repo":"parallax/jsPDF","slug":"the-option-pdfjsnewwindow-just-works-in-a-browser","errorCode":null,"errorMessage":"The option pdfjsnewwindow just works in a browser-environment.","messagePattern":"The option pdfjsnewwindow just works in a browser-environment\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":3208,"sourceCode":"            pdfViewer.height = \"400px\";\n            pdfViewer.src =\n              pdfJsUrl +\n              pdfJsQueryChar +\n              \"file=&downloadName=\" +\n              encodeURIComponent(options.filename);\n\n            pdfViewer.onload = function() {\n              PDFjsNewWindow.document.title = options.filename;\n              pdfViewer.contentWindow.PDFViewerApplication.open(\n                scope.output(\"bloburl\")\n              );\n            };\n\n            initializedPdfJsWindow.body.appendChild(pdfViewer);\n          }\n          return PDFjsNewWindow;\n        } else {\n          throw new Error(\n            \"The option pdfjsnewwindow just works in a browser-environment.\"\n          );\n        }\n      case \"dataurlnewwindow\":\n        if (\n          Object.prototype.toString.call(globalObject) === \"[object Window]\"\n        ) {\n          var dataURLNewWindow = globalObject.open();\n          if (dataURLNewWindow !== null) {\n            var initializedDataUrlWindow = initializeNewWindow(\n              dataURLNewWindow\n            );\n            var dataUrlFrame = initializedDataUrlWindow.document.createElement(\n              \"iframe\"\n            );\n\n            dataUrlFrame.src = this.output(\"datauristring\", options);\n            initializedDataUrlWindow.body.appendChild(dataUrlFrame);","sourceCodeStart":3190,"sourceCodeEnd":3226,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L3190-L3226","documentation":"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.","triggerScenarios":"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.","commonSituations":"Server-side generation reused from browser code; Node-based PDF services; test environments without a DOM; Electron main process.","solutions":["Switch to a Node-safe output in non-browser environments: `doc.output('arraybuffer')` or `doc.output('datauristring')`.","Branch on `typeof window !== 'undefined'` before choosing the 'pdfjsnewwindow' target.","Serve the generated PDF bytes through your own HTTP endpoint and let a browser load PDF.js against that URL."],"exampleFix":"// before\ndoc.output('pdfjsnewwindow', { pdfJsUrl: '/viewer.html' }); // in Node\n// after\nconst isBrowser = typeof window !== 'undefined';\nisBrowser\n  ? doc.output('pdfjsnewwindow', { pdfJsUrl: '/viewer.html' })\n  : require('fs').writeFileSync('out.pdf', Buffer.from(doc.output('arraybuffer')));","handlingStrategy":"validation","validationCode":"const isBrowser = typeof window !== 'undefined' && Object.prototype.toString.call(window) === '[object Window]';\nif (isBrowser) {\n  doc.output('pdfjsnewwindow', { pdfJsUrl: '/viewer.html' });\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('pdfjsnewwindow', { pdfJsUrl }); }\ncatch (e) { return doc.output('arraybuffer'); }","preventionTips":["Detect environment before choosing a viewer-based output target.","Keep PDF.js viewer usage strictly on the client.","Provide a Node-safe fallback in shared code."],"tags":["environment","browser","output","node","pdfjs"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}