最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jsPDF is not defined error in html project? - Stack Overflow

programmeradmin6浏览0评论

I am implementing jsPdf example and have sample html project in which i included jquery and jspdf cdn link to generate pdf. Here is my code

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script> src = ".3.4/jspdf.min.js"</script>
    <script src=".2.4.min.js"></script>
</head>

<body>
    <div id="content">
        <p>Testing testing testing</p>
    </div>
    <button type="button" id="export">Export To Pdf</button>
    <script>
        var doc = new jsPDF();
        $('#export').click(function () {
            doc.fromHTML($('#content').html(), 15, 15, {
                'width': 170
            });
            doc.save('sample-file.pdf');
        });
    </script>
</body>

</html>

I am implementing jsPdf example and have sample html project in which i included jquery and jspdf cdn link to generate pdf. Here is my code

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script> src = "https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"</script>
    <script src="https://code.jquery./jquery-2.2.4.min.js"></script>
</head>

<body>
    <div id="content">
        <p>Testing testing testing</p>
    </div>
    <button type="button" id="export">Export To Pdf</button>
    <script>
        var doc = new jsPDF();
        $('#export').click(function () {
            doc.fromHTML($('#content').html(), 15, 15, {
                'width': 170
            });
            doc.save('sample-file.pdf');
        });
    </script>
</body>

</html>

But it is giving this error

Uncaught ReferenceError: jsPDF is not defined
at index.html:19

What's wrong in my code?

Share Improve this question asked Jan 3, 2019 at 9:55 Fahad SubzwariFahad Subzwari 2,3653 gold badges28 silver badges63 bronze badges 1
  • When importing a script, it will export the variable/reference you may use to work with (i.e. jsPDF). Make sure it's imported correctly and keep note of script loading via head and body. I would remend usage of webpack or requirejs to avoid cluttering of the global namespace. – Koshux Commented Jan 3, 2019 at 13:34
Add a ment  | 

2 Answers 2

Reset to default 4

The error is in this line-

<script> src = "https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"</script>

src should be inside the script tag

<script src = "https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>

For exporting and rendering

$('#export').click(() => {
    var pdf = new jsPDF('p','pt','a4');
    pdf.addHTML(document.body,function() {
        pdf.save('web.pdf');
    });
})

fix your script tag of https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js to

<script src="https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
发布评论

评论列表(0)

  1. 暂无评论