Jspdf: ReferenceError: window is not defined in NodeJs

Created on 3 Aug 2018  路  11Comments  路  Source: MrRio/jsPDF

Hi,i try to use jsPDF in Node but fount the error : ReferenceError: window is not defined

Are you using the latest version of jsPDF?
Yes锛宨 use "jspdf": "^1.4.1"
Have you tried using jspdf.debug.js?
Yes
Steps to reproduce
the environment : node 8.11.3

the code:

let jsPDF = require('jspdf');


var doc = new jsPDF()

doc.text('Hello world!', 10, 10)
doc.save('a4.pdf')

the error :

d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:171
Y=$.API,K=J=X="",Y.addMetadata=function(t,e){return J=e||"http://jspdf.default.namespaceuri/",X=t,this.internal.events.subscribe("postPutResources",function(){if(X){var t='<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:jspdf="'+J+'"><jspdf:metadata>',e=unescape(encodeURIComponent('<x:xmpmeta xmlns:x="adobe:ns:meta/">')),n=unescape(encodeURIComponent(t)),r=unescape(encodeURIComponent(X)),i=unescape(encodeURIComponent("</jspdf:metadata></rdf:Description></rdf:RDF>")),o=unescape(encodeURIComponent("</x:xmpmeta>")),a=n.length+r.length+i.length+e.length+o.length;K=this.internal.newObject(),this.internal.write("<< /Type /Metadata /Subtype /XML /Length "+a+" >>"),this.internal.write("stream"),this.internal.write(e+n+r+i+o),this.internal.write("endstream"),this.internal.write("endobj")}else K=""}),this.internal.events.subscribe("putCatalog",function(){K&&this.internal.write("/Metadata "+K+" 0 R")

ReferenceError: window is not defined
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:171:8032
    at vt (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:146)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:210)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
Waiting for the debugger to disconnect...
ReferenceError: window is not defined
jspdf.min.js:171
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:171:8032
    at vt (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:146)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.min.js:1:210)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

when i use jspdf.debug.js

d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:11776
  }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || window.content || window));
                                                                                      ^

ReferenceError: window is not defined
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:11776:87
    at _typeof (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:2:83)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:5:2)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
Waiting for the debugger to disconnect...
ReferenceError: window is not defined
jspdf.debug.js:11776
    at d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:11776:87
    at _typeof (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:2:83)
    at Object.<anonymous> (d:\Users\user\js\hello\node_modules\jspdf\dist\jspdf.debug.js:5:2)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

Ideally a link too. Try fork this http://jsbin.com/rilace/edit?html,js,output

What I saw

What I expected

Most helpful comment

For those using jspdf with server rendering: I fixed this issue by using require instead of import statement and wrapped in a function.
e.g

function savePdf() {
      const jsPDF = require('jspdf')
      const pdf = new JSPDF('p', 'mm', 'a4')
      pdf.addImage(imgBlob, 'PNG', 0, 0)
      pdf.save('test.pdf')
}

All 11 comments

I'm seeing the same issue with 1.4.1 and node 10.7.0. Just requiring the module is enough to trigger the error:

let jsPDF = require('jspdf');

You could try using pdfkit, I had the same problem and this help me a lot
I make the PDF on my server side
https://www.npmjs.com/package/pdfkit

Duplicate of #1244

I'm still having this issue. Did someone managed to solve it?
jsPDF v1.5.2
node v8.11.3

Did you try jspdf.node.debug.js?

What did you get for error messages?

Still occurs with Node v8.11.3 and the 20 January 2019 version of jsPDF.

This (with jspdf.debug) did just work for me.

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};
var encoding = require('encoding')
var fs = require('fs');
jsPDF = require ("jspdf.debug");
var doc = new jsPDF()
doc.text('Hello world!', 10, 10)
// Did not work --> doc.save('a4.pdf')
var data = doc.output()
var buffer = encoding.convert(data, "Latin_1")
fs.writeFileSync('hello.pdf', buffer);

For those using jspdf with server rendering: I fixed this issue by using require instead of import statement and wrapped in a function.
e.g

function savePdf() {
      const jsPDF = require('jspdf')
      const pdf = new JSPDF('p', 'mm', 'a4')
      pdf.addImage(imgBlob, 'PNG', 0, 0)
      pdf.save('test.pdf')
}

Getting the error as well

node src/core/pdf.js                                                                                                                                                                  

/home/xotonic/xotonic-site/node_modules/jspdf/dist/jspdf.min.js:37                                                                                                                      
(function(t,e){var A,n=1,S=function(t){return t.replace(/\\/g,"\\\\").replace(/\(/g,"\\(").replace(/\)/g,"\\)")},y=function(t){return t.replace(/\\\\/g,"\\").replace(/\\\(/g,"(").repl 

ReferenceError: window is not defined                                                                                                                                                   
    at /home/xotonic/xotonic-site/node_modules/jspdf/dist/jspdf.min.js:37:30237                                                                                                         
    at /home/xotonic/xotonic-site/node_modules/jspdf/dist/jspdf.min.js:1:124                                                                                                            
    at Object.<anonymous> (/home/xotonic/xotonic-site/node_modules/jspdf/dist/jspdf.min.js:1:128)                                                                                       
    at Module._compile (module.js:652:30)                                                                                                                                               
    at Object.Module._extensions..js (module.js:663:10)                                                                                                                                 
    at Module.load (module.js:565:32)                                                                                                                                                   
    at tryModuleLoad (module.js:505:12)                                                                                                                                                 
    at Function.Module._load (module.js:497:3)                                                                                                                                          
    at Module.require (module.js:596:17)                                                                                                                                                
    at require (internal/module.js:11:18)                                                                                                                                               
shell returned 1  

pdf.js:

const jsPDF = require('jspdf')     
const doc = new jsPDF()            
doc.text('Hello World', 1, 1)      
doc.save('../../public/resume.pdf')

Environment:

$ npm -v
3.5.2 
$ node -v
v8.10.0

jspdf": "^1.5.3",

For those using jspdf with server rendering: I fixed this issue by using require instead of import statement and wrapped in a function.
e.g

function savePdf() {
      const jsPDF = require('jspdf')
      const pdf = new JSPDF('p', 'mm', 'a4')
      pdf.addImage(imgBlob, 'PNG', 0, 0)
      pdf.save('test.pdf')
}

This method (require vs. import) worked for me, using SSR (universal) Nuxt.js.

Thanks @sundayguru!

@adabutch, me i am using this package in node js but still not working,

Was this page helpful?
0 / 5 - 0 ratings