Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

javascript - 调用jsPDF的保存方法时获取TypeError Arg 1(Getting TypeError Arg 1 when calling save method of jsPDF)

I have a very simple implementation of jsPDF in a react project I'm currently working on.(在我目前正在从事的React项目中,我有一个非常简单的jsPDF实现。)

Just trying to run the sample 'hello world' code but the save method throws the following error:(只是尝试运行示例“ hello world”代码,但save方法会引发以下错误:)

在此处输入图片说明

My code is very simple, first, import the lib like this:(我的代码非常简单,首先,像这样导入lib:)

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.(上面的错误在save()上触发,但我不知道到底是什么原因引起的。)

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.(UPDATE我刚刚注意到,如果我在我的应用程序组件的根目录中调用方法,则代码可以正常工作,当从其他组件中调用它时,就会出现问题。)

Here is my setup(这是我的设置)

<App>
  <Sketch>
    <SketchControls>
    <IOControls>

exportDrawing() is declared in <Sketch> and the its triggered from a button in <IOControls>(在<Sketch>声明exportDrawing() ,并通过<IOControls>的按钮触发它)

  ask by Ricardo Sanchez translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.(我正在使用paperjs库作为绘图引擎。)

I called the paper.install(window) method that gives scope to the window DOM element.(我调用了paper.install(window)方法,该方法将范围赋予窗口DOM元素。) By removing this call jspdf works.(通过删除此调用, jspdf可以工作。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...