gpt4 book ai didi

node.js - jspdf 模块每次都会崩溃,并出现 ReferenceError : window is not defined

转载 作者:太空宇宙 更新时间:2023-11-03 22:24:24 27 4
gpt4 key购买 nike

我正在开发一个小项目,该项目在 Node 和express中生成pdf,并一直在尝试使用jspdf npm模块,但不知何故,每当我安装该包并需要它时,它都会使我的服务器崩溃。这是我在 server.js 文件中的要求:

var jsPDF = require('jspdf')

这是当我尝试运行我的服务器时它给我的响应:

(window.AcroForm=function(t){var n=window.AcroForm;n.scale=function(t) {return t*(r.internal.scaleFactor/1)},n.antiScale=function(t){return 1/r.internal.scaleFactor*t};var r={fields:[],xForms:[],acroFormDictionaryRoot:null,printedOut:!1,internal:null};e.API.acroformPlugin=r;var i=function(){for(var t in this.acroformPlugin.acroFormDictionaryRoot.Fields){var e=this.acroformPlugin.acroFormDictionaryRoot.Fields[t];e.hasAnnotation&&a.call(this,e)}},o=function(){if(this.acroformPlugin.acroFormDictionaryRoot)throw new Error("Exception while creating AcroformDictionary");this.acroformPlugin.acroFormDictionaryRoot=new n.AcroFormDictionary,this.acroformPlugin.internal=this.internal,this.acroformPlugin.acroFormDictionaryRoot._eventID=this.internal.events.subscribe("postPutResources",l),this.internal.events.subscribe("buildDocument",i),this.internal.events.subscribe("putCatalog",c),this.internal.events.subscribe("pos

ReferenceError: window is not defined

最佳答案

jsPDF库用于客户端(Web 浏览器),这就是为什么它期望存在窗口变量。幸运的是,有人已经回答了如何使这项工作在服务器端 here 。摘自该答案:

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};

var fs = require('fs');
var jsPDF = require('jspdf');

var doc = new jsPDF();
doc.text("Hello", 10, 10);
var data = doc.output();

fs.writeFileSync('./document.pdf', data);

delete global.window;
delete global.navigator;
delete global.btoa;

关于node.js - jspdf 模块每次都会崩溃,并出现 ReferenceError : window is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48736013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com