【问题标题】:jsPDF is not defined when saving div to PDF将 div 保存为 PDF 时未定义 jsPDF
【发布时间】:2020-12-01 09:33:38
【问题描述】:

我正在尝试使用 jsPDF 保存具有特定 id 的 div 元素,但得到了Uncaught ReferenceError: jsPDF is not defined。尝试了很多不同的方法来解决这个问题,包括 stackoverflow 上所有可用的解决方案,但都没有奏效。

这是 div 元素的外观(它是一个根据用户选择自动填充的表格,我删除了大部分选项以减少此示例的行数)。下面是一个快速的 sn-p,可以让您了解发生了什么。它仍然说 jsPDF 没有定义:

$('#savePDF').click(function() {
  var pdf = new jsPDF('p', 'pt', 'letter');
        source = $('#yourSummary');
        specialElementHandlers = {
            '#bypassme': function (element, renderer) {
                return true
            }
        };
        margins = {
            top: 80,
            bottom: 60,
            left: 40,
            width: 522
        };
        pdf.fromHTML(
            source,
            margins.left,
            margins.top, {
                'width': margins.width,
                'elementHandlers': specialElementHandlers
            },

            function (dispose) {
                pdf.save('your-summary.pdf');
            }, margins
        );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.umd.min.js"></script>
<div id="yourSummary" class="modal-body">
  <table id="yourExterior" class="table table-hover">
  <h5>Exterior</h5>
    <tbody>
     <tr>
      <th scope="row">Body colour</th>
      <td id="sumBC"></td>
     </tr>
    </tbody>
  </table>
</div>
<button class="btn-primary" id="savePDF">Save configuration as PDF</button>

但是一旦我在浏览器中按下按钮,就会出现以下错误:

Uncaught ReferenceError: jsPDF is not defined
at HTMLButtonElement.<anonymous> (main.js:34)
at HTMLButtonElement.dispatch (jquery-3.5.1.slim.min.js:2)
at HTMLButtonElement.v.handle (jquery-3.5.1.slim.min.js:2)

我做错了什么以及如何解决这个问题?

【问题讨论】:

  • 在这里建立一个工作示例可能会有所帮助,最好使用stack snippet。这可能会让我们更好地了解出了什么问题。
  • @showdev 感谢您的建议,刚刚用 sn-p 更新了问题,它给出了同样的错误。

标签: javascript jquery pdf-generation jspdf referenceerror


【解决方案1】:

使用以前的版本
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"&gt;&lt;/script&gt;

2.0.0 版本非常不同。
例如,您将使用 jspdf.jsPDF 而不是方法“jsPDF”,但您会看到新的错误。

【讨论】:

  • 仅供参考:jsPDF release 2.0.
  • 感谢您的提醒,因为这些信息应该在他们的文档中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2021-12-15
  • 1970-01-01
相关资源
最近更新 更多