gpt4 book ai didi

javascript - jsPDF 服务器端 (node.js) 使用 node-jspdf

转载 作者:数据小太阳 更新时间:2023-10-29 05:03:52 27 4
gpt4 key购买 nike

我实际上是在尝试在服务器端包含 jspdf,然后将其用于简单的 pdf 生成(只是文本“Hello world!”)(转到 url - 获取 pdf localhost:8080)。现在我面临的第一个问题是

  • 如何包含它/如何在 Node 中使用 jsPDF?
  • 尝试使用 npm install node-jspdf 安装它时出现以下错误-

> G:\test\myproj>npm install node-jspdf
node-jspdf@0.0.3 install G:\test\myproj\node_modules\node-jspdf
sh install.sh
'sh' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs
\\node_modules\\npm\\bin\\npm-cli.js" "install" "node-jspdf"
npm ERR! node v0.12.4
npm ERR! npm v2.10.1
npm ERR! code ELIFECYCLE

npm ERR! node-jspdf@0.0.3 install: `sh install.sh`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-jspdf@0.0.3 install script 'sh install.sh'.
npm ERR! This is most likely a problem with the node-jspdf package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! sh install.sh
npm ERR! You can get their info via:
npm ERR! npm owner ls node-jspdf
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! G:\test\myproj\npm-debug.log

任何人都可以帮助我如何将它包含在 Node 中吗?并给出一个4-5行的demo。

最佳答案

您实际上可以直接使用 jspdf(npm install jspdf 而不是 npm install node-jspdf)。 Jspdf 当前 (v1.3.2) 构建时并未考虑 Node 支持,但您可以像下面这样模拟全局变量并使其以这种方式工作。这是一个基本示例,jspdf 的所有功能将不可用。

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.html2pdf = {};
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, 'binary');

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

关于javascript - jsPDF 服务器端 (node.js) 使用 node-jspdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30694428/

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