Owen Davey
Owen Davey

Reputation: 1212

doc.save() throwing error with jspdf

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?

Upvotes: 28

Views: 33755

Answers (4)

jiban
jiban

Reputation: 11

<!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>

Upvotes: -2

Kyle Baker
Kyle Baker

Reputation: 3712

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.

Upvotes: 2

Lesram
Lesram

Reputation: 15

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

Upvotes: -2

Owen Davey
Owen Davey

Reputation: 1212

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

Upvotes: 51

Related Questions