admin管理员组文章数量:1193969
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:\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
Can anybody help me on how to include it in node? And give a 4-5 line demo.
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:\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
Can anybody help me on how to include it in node? And give a 4-5 line demo.
Share Improve this question asked Jun 7, 2015 at 14:04 user2736738user2736738 30.9k5 gold badges44 silver badges57 bronze badges 1- @Eric.: Go to github repo and download – user2736738 Commented Oct 23, 2015 at 16:43
5 Answers
Reset to default 16UPDATE: Jspdf now support nodejs so should be able to import jspdf without having to add the browser globals as outlined in this answer.
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;
In extension to the answer provided by Simon Bengtsson:
I managed to handle even latin-1 characters by sending the output of jsPdf to encoding:
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;
I am able to install node-jspdf in Windows.
- As suggested download jspdf-master.zip and extract it.
Now use the command:
npm install <location on jspdf-master extract>
After that you can use npm list
to see the installation success.
Node-jspdf
mainly uses library jsPDF as the core library to render PDF file
but node-jspdf
can only be installed on *unix
system therefore you can download and install jsPDF
manually by following steps in this file
I think you only need to install jsPDF
to work with PDF file.
Download your jspdf folder from parallax jspdf. After installing jspdf ( "npm install jspdf" ) you get the jspdf folder inside node-modules. Go inside it and replace the jspdf.min.js by the jspdf.amd.min.js present in the /dist folder inside the jspdf downloaded from parallax
本文标签: javascriptjsPDF server side (nodejs) usage using nodejspdfStack Overflow
版权声明:本文标题:javascript - jsPDF server side (node.js) usage using node-jspdf - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738459516a2087949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论