Skip to content

ReferenceError: window is not defined in NodeJs #1891

@Hxuhao233

Description

@Hxuhao233

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,i 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions