本文介绍了使用node-jspdf的jsPDF服务器端(node.js)用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am actually trying to include jspdf in server side and then use it for simple pdf generation(simply the text "Hello world!")(Go to the url- get the pdf localhost:8080). Now the first problem I face is


  • 如何包含它/如何在节点中使用jsPDF?

  • 尝试使用 npm install node-jspdf 进行安装时,会出现以下错误 -

  • How to include it / What to do to use jsPDF in node?
  • While trying to install it using npm install node-jspdf then it gives following error-
> 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


有人可以帮助我如何将它包含在节点中吗?并提供4-5行演示。

Can anybody help me on how to include it in node? And give a 4-5 line demo.

推荐答案

您实际上可以直接使用jspdf( npm install jspdf 而不是 npm install node -jspdf )。 Jspdf目前(v1.3.2)没有考虑到节点支持而构建,但你可以像下面那样模拟全局变量并让它以这种方式工作。这是一个基本示例,jspdf的所有功能都不可用。

You can actually use jspdf directly (npm install jspdf instead of npm install node-jspdf). Jspdf is currently (v1.3.2) not built with node support in mind, but you can mock the globals like below and get it to work that way. This is a basic example and all features of jspdf will not be available.

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-jspdf的jsPDF服务器端(node.js)用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:33
查看更多