Using jsPDF server (node.js) using node-jspdf
I am actually trying to include jspdf on the server side and then use it to generate a simple pdf (just the text "Hello world!") (Go to url-get pdf localhost: 8080). Now the first problem I am facing is -
- How do I enable it / What do I do to use jsPDF in node?
- When trying to install it using,
npm install node-jspdf
it gives the 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
Can someone help me on how to include it in node? And give a 4-5 line demo.
source to share
You can actually use jspdf directly ( npm install jspdf
instead of npm install node-jspdf
). Currently Jspdf (v1.3.2) is not built with node support in mind, but you can mock globals like below and make them work that way. This is a basic example and all jspdf functionality will be unavailable.
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;
source to share
I can install node-jspdf on 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 success of the installation.
source to share
In addition to the answer provided by Simon Bengtson:
I was able to handle even latin 1 characters by sending jsPdf output 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;
source to share