【问题标题】:TypeError: doc.fromHTML is not a function类型错误:doc.fromHTML 不是函数
【发布时间】:2016-09-17 17:00:09
【问题描述】:

所以我正在运行此代码以使用 jsPDF 在页面上打印部分,但他一直返回

TypeError: doc.fromHTML 不是函数

function printPDF(object){
    var section_id = jQuery(object).parent().find('p').text().substr(0,3).replace('.','_');
    if(!jQuery('#contentsection'+section_id).length){
        jQuery(object).parent().parent().parent().before('<div id="contentsection'+section_id+'">'+'</div>');
    }
    var title_block = jQuery(object).parent().parent().parent();
    var text_under_title = jQuery(object).parent().parent().parent().next();
    jQuery('#contentsection'+section_id).append(title_block);
    jQuery('#contentsection'+section_id).append(text_under_title);

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

    jQuery('.print-to-pdf').click(function () {
        doc.fromHTML(jQuery('#contentsection'+section_id).html(), 15, 15, {
            'width': 170,
           // 'elementHandlers': specialElementHandlers
        });
        doc.save('test-file.pdf');
    });

}

知道发生了什么或知道我做错了什么的人,我也不知道这些“元素处理程序”是什么。

【问题讨论】:

  • 我没有看到任何 printPDF() 调用...“doc”对象在那里定义。而且,似乎 printPDF 函数缺少一个结束括号( } )...从您在此处显示的内容来看。
  • 在 onclick 中调用 printPDF,如下函数 appendPdf($){ $('.heading').append(''); }
  • 我很难重现这个问题...你能发布你的 HTML 示例吗?
  • 我需要它...因为我在本地测试中“伪造”了一个父层次结构和一个 p id。 PDF 已创建……奇怪的是,第二次点击两次。

标签: javascript wordpress printing jspdf


【解决方案1】:

当您导入 jspdf.js 而不是 jspdf.debug.js 时会发生这种情况。后者没有 fromHTML() 函数,因此会引发错误。

包括适当的库。

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>

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

确保引用中不存在 jspdf.js。

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 2016-02-26
    • 2013-04-01
    • 2016-03-06
    • 2018-04-28
    • 2019-02-14
    • 2021-10-02
    • 2021-10-30
    相关资源
    最近更新 更多