Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - jsPDF server side (node.js) usage using node-jspdf

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

  • 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:estmyproj>npm install node-jspdf
 [email protected] install G:estmyproj
ode_modules
ode-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:estmyproj
pm-debug.log

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...