Javascript 未捕获的 ReferenceError: jsPDF 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43333286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:50:43  来源:igfitidea点击:

Uncaught ReferenceError: jsPDF is not defined

javascripthtmljspdf

提问by D. A

So this is my HTML code where I'm trying to get whatever is outputted in the div "abc" to a downloadable pdf file.

所以这是我的 HTML 代码,我试图将 div "abc" 中输出的任何内容转换为可下载的 pdf 文件。

<!-- Page Heading -->
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">
            Reports
        </h1>
        <ol class="breadcrumb">
            <li>
                <i class="fa fa-dashboard"></i>  <a href="dashboard">Dashboard</a>
            </li>
            <li class="active">
                <i class="fa fa-edit"></i> Reports
            </li>

        </ol>
        <body>
          <div id="abc">
</div>
<div id="editor"></div>
<button id="cmd">Download Survey Report</button>
        <div id="abc">

        </div>
        </body>

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

                var doc = new jsPDF();
                var specialElementHandlers = {
                  '#editor': function (element, renderer) {
                return true;
            }
        };

        $('#cmd').click(function () {
            doc.fromHTML($('#abc').html(), 15, 15, {
                'width': 170,
                    'elementHandlers': specialElementHandlers
            });
            doc.save('SurveyReport.pdf');
        });
        </script>


        <script>
        firebase.auth().onAuthStateChanged(function(user){
          if(user){
            var userID = user.uid;
            RetrieveSurvey(userID,1,PrintSurvey(document.getElementById("abc")));
          }
        })
        </script>

    </div>
</div>

And I'm getting this error on the console: "Uncaught ReferenceError: jsPDF is not defined"

我在控制台上收到此错误:“未捕获的 ReferenceError:jsPDF 未定义”

I am not sure what I'm doing wrong here, I even added jsPDF in the script tag...

我不确定我在这里做错了什么,我什至在脚本标签中添加了 jsPDF ......

回答by loelsonk

First of all your HTML is invalid. Check W3S.

首先,您的 HTML 无效。检查W3S

Put your link to jspdfinside <head>tag, you can do it even inside <body>tag.

将您的链接jspdf放在<head>标签内,您甚至可以在<body>标签内进行。

Replace your jspdfscript with latest one:

jspdf用最新的脚本替换你的脚本:

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

回答by Luiz H. Alves Ferreira

use this:

用这个:

window.jsPDF = require('jspdf');

instead of this:

而不是这个:

import jsPDF from 'jspdf';