I would like to know how to incorporate an external javascript library into a response project. For example, I would like to import the jspdf library: https://github.com/MrRio/jsPDF/blob/master/jspdf.js and use it in my application.
So far I have been trying to use like this:
let React = require('react'); let { Spacing, Typography } = Styles; let DoughnutChart = require("react-chartjs").Doughnut; let Chart = require('react-google-charts').Chart; let { StylePropable, StyleResizable } = Mixins; let EditableDiv = require('../EditableDiv.jsx'); //some other library //the library that matter for me var pdfConverter = require('../utils/jspdf.source.js'); //then I have my classical react code which works and a function to generate ad pdf _generatePdf: function(){ console.log('Genrating pdf'); var doc = new pdfConverter.jsPDF('p','pt'); var img = new Image(); }
I have the following error: TypeError: pdfConverter.jsPDF is not a function .
To make it work, I did something ugly, I copied the jspdf-source.js source to my response.jsx file and simply called jsPDF instead of pdfConverter.jsPDF. This is definitely not the right way, but cannot successfully import and use the library.
Can you tell me what I am doing wrong, and how can I fix it? thank you
-Edit -
When I used my ugly solution (copying the source to a file), I just needed to do the following:
var doc = new jsPDF('p','pt);
And it worked perfectly, expect I had a very large file
After the proposed solution from @dannyjolie below, I imported jspdf directly from the npm package, but I still cannot use the library. I tried the following error code:
var pdfConverter = require('jspdf'); var converter = new pdfConverter(); var doc = converter.jsPDF('p', 'pt');
TypeError: pdfConverter is not a constructor Does this mean that I need to import jsPDF from a package, not just jspdf?
Then i try
let pdfConverter = require('jspdf'); var converter = pdfConverter.jsPDF; var doc = new converter('p', 'pt');
ReferenceError: jsPDF not defined
TypeError: the converter is not a constructor
Well, obviously, I am not importing the right thing or the wrong one. What am I doing wrong?