所以我正在运行此代码以使用 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');
});
}
有人知道发生了什么或知道我做错了什么,我也不知道这些“elementHandlers”是什么。
当您导入 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。