nodejs, jsPDF and Windows

Asked

Viewed 326 times

0

I am unable to install jsPDF in my nodejs with npm. I have npm install node-jspdf to work with nodejs, but does not install because it gives problem when running the file install.sh. Even specifying the version of the package gives problem. So I downloaded the zip from Node-jspdf (indicated in the file install.sh), unzipped and created the necessary folders, indicated in the same installation file. When running the app the module is not located. However I was able to generate a PDF referencing the module directly, but I don’t know if it works for later:

HOW 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());

The PDF is created correctly in the local folder. But I intend to have it sent to the browser. But if I use the example of Githud, as shown below, the error "Navigator is not defined":

SO THE APP GAVE 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) Wouldn’t this "Navigator is not defined" error be because I’m just running a script? I guess running via browser will stop the problem...

  • you have the most updated version of Node? seems not. type node -v and say what the version is

  • Npm: '4.4.4', Ode: '6.10.2'

  • tried to install the node-jspdf? navigator is a browser variable

  • yes, I did as you indicated npm install node-jspdf --save. There is error in the file install.sh, but I opened it and I did what I needed. So I created the file with the default "hello word" code, as indicated on the Node-jspdf page, but it gives this error. Then I executed the code as indicated above (what worked), with this exception of having to indicate the relative path, being then the two doubts pending...

2 answers

0

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

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

I hope it works now, hugs!

  • Guilherme, but jsPDF has been around for a long time and I imagine it should be compatible with my version, right? Besides, if it only worked in the most updated version of Node, I imagine there would be a lot of people complaining too, but I could not find complaints of this type. Another thing: where I will use what I am developing, it has to be a Node 4.2 if I am not mistaken. And if jsPDF doesn’t run in this version then it won’t even work for me...

  • The command npm install <pkg> installs the latest version tag by default, making perfect sense the latest version of pkg requires the latest version of Node. What you can do is install a version with a tag oldest. link from releases is https://github.com/MrRio/jsPDF/releases and to install use npm install [email protected], for example.

  • Great William! It looks like you have now installed it without any problems, but you still have an error running the simplest example of "Hello world" that is on jsPDF’s own Github. When I run that example with var jsPDF = require('jspdf'); at the beginning, the message appears: Referenceerror: window is not defined. If I use var jsPDF = require('node-jspdf'); as I have seen in some examples, the error appears Cannot find module 'Node-jspdf'.

  • this variable windows It’s just for the client-side application. I’ve never used jspdf, but according to the documentation, jspdf is clientside and Node-jspdf is server-side. as you want for Ode, then you must install node-jspdf. I’ll edit my answer

  • Guilherme, I was able to partially solve the problem, because I had to indicate the path of the module directly. I changed my question too. Give a check when you can please and thanks again for the attention.

0


For registration only: I was able to run the PDF generation, but not via normal installation with npm install. I did the following: 1) I downloaded the jsPDF master ZIP file on Github, in the Releases the version compatible with my version of Nodejs, because I am not using the most current one (the reason is not the case); 2) I unzipped the file and put it right in the folder node_modules of my project; 3) In the code I pointed the package directly in 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 on my Windows installation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.