我实际上试图在服务器端包含jspdf,然后使用它进行简单的pdf生成(只需文本“Hello world!”)(转到url-获取pdf localhost:8080)。现在我面临的第一个问题是
npm install node-jspdf
安装它时,会出现以下错误 - > G:\test\myproj>npm install node-jspdf
[email protected] 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! [email protected] install: `sh install.sh` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] 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
任何人都可以帮助我如何将其包含在节点中吗?并给出 4-5 行演示。
更新: Jspdf 现在支持 NodeJS,因此应该能够导入 jspdf,而无需添加本答案中概述的浏览器全局变量。
您实际上可以直接使用jspdf(
npm install jspdf
而不是npm install node-jspdf
)。 Jspdf 目前(v1.3.2)并未考虑到节点支持,但您可以像下面一样模拟全局变量并让它以这种方式工作。这是一个基本示例,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;
扩展 Simon Bengtsson 提供的答案:
我通过将 jsPdf 的输出发送到编码来处理甚至 latin-1 字符:
global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};
var fs = require('fs');
var jsPDF = require('jspdf');
var encoding = require('encoding')
var doc = new jsPDF();
doc.text("HelloäöüßÄÖÜ©µ®", 10, 10);
var data = doc.output()
var buffer = encoding.convert(data, "Latin_1")
fs.writeFileSync('./document.pdf', buffer);
delete global.window;
delete global.navigator;
delete global.btoa;
我可以在 Windows 中安装 node-jspdf。
现在使用命令:
npm install <location on jspdf-master extract>
之后就可以使用
npm list
查看安装成功了。
Node-jspdf
主要使用库jsPDF作为核心库来渲染PDF文件
但是
node-jspdf
只能安装在*unix
系统上,因此您可以按照此文件中的步骤手动下载并安装
jsPDF
我认为你只需要安装
jsPDF
即可处理 PDF 文件。
从 parallax jspdf 下载 jspdf 文件夹。安装 jspdf(“npm install jspdf”)后,您将在节点模块内获得 jspdf 文件夹。进入其中,将 jspdf.min.js 替换为从 parallax 下载的 jspdf 中 /dist 文件夹中的 jspdf.amd.min.js