nodejs, jsPDF, and Windows

0

I'm not able to install jsPDF on my nodejs with npm. I ran npm install node-jspdf to work with nodejs, but it does not install because it gives trouble to run install.sh . Even specifying the package version gives problem. So I downloaded the zip of node-jspdf (indicated in file install.sh ), unpacked it and created the necessary folders, indicated in the same installation file. When running the app the module is not found. But I was able to generate a PDF by referencing the module directly, but I do not know if this works for later:

SO THE APP WORKED IN PRINCIPLE:

var fs = require('fs');
var path = './jsPDF/';
jsPDF = require(path + 'jspdf');

var doc = new jsPDF();
doc.text('Hello world!', 10, 10);
fs.writeFile('teste.pdf', doc.output());

PDF is created correctly in the local folder. But I want to get it sent to the browser. But if I use the Githud example, the following error appears: "navigator is not defined":

SO THE APP DONE ERROR

var fs = require('fs');
var path = './jsPDF/';
jsPDF = require(path + 'jspdf');

var doc = new jsPDF();
doc.text('Hello world!', 10, 10);

doc.save('Test.pdf', function(err){console.log('saved!');});  // <<<<<<==== Gera o erro

Questions: 1) Can I use jspdf indicating the module path directly and saving the file the way it worked? 2) Is not this "navigator is not defined" error because I'm just running a script? I imagine that if running via browser will stop the problem ...

    
asked by anonymous 09.07.2017 / 03:25

2 answers

0

For registration only: I was able to make PDF generation work, but I did not see normal installation with npm install . I did the following: 1) I downloaded the master zip file of jsPDF in Github, in the Releases version compatible with my version of NodeJS, because I am not using the most current (not the case the reason); 2) I unzipped the file and put it right in the node_modules folder of my project; 3) In the code I pointed the package directly to the relative path: require('./meu_path_relativo/node_modules/pasta_jsPDF/jspdf') ;

I know it's not the right strategy, but it was the only way to make it work in my Windows installation.

    
12.07.2017 / 17:32
0

According to the documentation, jsPDF is a package client-side . Since you're working with node , then the package they recommend is node-jspdf . For this, I recommend uninstalling your jspdf and installing node-jspdf through:

npm unistall jspdf --save
npm install node-jspdf --save

Hope it works now, hugs!

    
09.07.2017 / 17:18