doc.save() throwing error with jspdf

妖精的绣舞 提交于 2019-12-12 09:28:39

问题


When I try and use the save() function for jsPDF it's throwing the following error:

ReferenceError: saveAs is not defined

I'm just trying a very simple example:

var doc = new jsPDF();
doc.text(20, 20, 'Hello world!');
doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.');
doc.addPage();
doc.text(20, 20, 'Do you like that?');

doc.save('test.pdf');

Anyone have any ideas what's wrong?


回答1:


Found out what the issue was. The saveAs function is part of FileSaver.js so I just needed to include this and everything worked.




回答2:


I don't have enough to comment yet, so I'm adding this as an answer... saveAs() is a w3c interface. If adding FileSaver fixed your issue, that means you were using an old browser; FileSaver.js is used as a shim for jsPDF to support older browsers that don't have that function natively.




回答3:


You can also use jspdf.debug.js it contains all of the plugins in one file. This way you are covered if something else is needed.

-Cheers




回答4:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hello world</title>
</head>
<body>
    <h1>Hello world</h1>
    <script type="text/javascript" src="jspdf.min.js"></script>
    <script type="text/javascript">
        var pdf = new jsPDF();
        pdf.text(30, 30, 'Hello world!');
        pdf.save('hello_world.pdf');
    </script>
</body>
</html>


来源:https://stackoverflow.com/questions/20340194/doc-save-throwing-error-with-jspdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!