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

use*_*738 6 javascript node.js jspdf

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

  • 如何包含它/如何在节点中使用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
Run Code Online (Sandbox Code Playgroud)
'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
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我如何将其包含在节点中?并提供4-5线演示.

Sim*_*son 12

您实际上可以直接使用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;
Run Code Online (Sandbox Code Playgroud)


Tho*_*Tho 0

Node-jspdf主要使用jsPDF库作为核心库来渲染PDF文件

node-jspdf只能安装在系统上,因此您可以按照此文件*unix中的步骤手动下载并安装jsPDF

我认为您只需要安装jsPDF即可使用 PDF 文件。