Reputation: 5167
I have a very simple implementation of jsPDF in a react project I'm currently working on. Just trying to run the sample 'hello world' code but the save method throws the following error:
My code is very simple, first, import the lib like this:
import jsPDF from 'jspdf';
Then on a click event function from a Component
<IOControls exportDrawing={this.exportDrawing} />
I run:
exportDrawing() {
const doc = new jsPDF();
doc.text('Hello World!', 10, 10);
console.log(doc); // shows an object
doc.save('a4.pdf');
}
The above error triggers on save() but I have no idea what exactly is causing it.
Any help will be much appreciated.
UPDATE I just noticed that if I call the methods at the root of my app component the code works fine, the problem ocurrs when it is called from within other components.
Here is my setup
<App>
<Sketch>
<SketchControls>
<IOControls>
exportDrawing()
is declared in <Sketch>
and the its triggered from a button in <IOControls>
Upvotes: 4
Views: 153
Reputation: 5167
It turns out that there was a method form another library causing a scope issue.
I'm using the paperjs library as my drawing engine. I called the paper.install(window) method that gives scope to the window DOM element. By removing this call jspdf works.
Upvotes: 2