我尝试使用jsPDF从html源代码创建pdf。
消息来源如下:
但发现PDF的结果如下:
这是使用的代码
HTML源
<div class="box-body" id="list-todo">
<!-- See dist/js/pages/dashboard.js to activate the todoList plugin -->
<ul class="todo-list ui-sortable">
<li class="bg-aqua "><b>Your tasks</b></li>
<li style="overflow: hidden">
<!-- drag handle -->
<span class="handle ui-sortable-handle"><i class="fa fa-ellipsis-v"></i> <i class="fa fa-ellipsis-v"></i></span>
<!-- checkbox -->
<input type="checkbox" value="">
<!-- todo text -->
<span class="text">70 % | Set groups</span>
<!-- Emphasis label -->
<small class="label label-warning" style="float: right;white-space: nowrap"><i class="fa fa-clock-o"></i> 2018-06-30</small>
</li>
<li style="overflow: hidden">
<!-- drag handle -->
<span class="handle ui-sortable-handle"><i class="fa fa-ellipsis-v"></i> <i class="fa fa-ellipsis-v"></i></span>
<!-- checkbox -->
<input type="checkbox" value="">
<!-- todo text -->
<span class="text">50 % | set tasks</span>
<!-- Emphasis label -->
<small class="label label-primary" style="float: right;white-space: nowrap"><i class="fa fa-clock-o"></i> 2018-06-30</small>
</li>
<hr>
<li class="bg-aqua"><b>General tasks</b></li>
<li style="overflow: hidden">
<!-- drag handle -->
<span class="handle ui-sortable-handle"><i class="fa fa-ellipsis-v"></i> <i class="fa fa-ellipsis-v"></i></span>
<!-- checkbox -->
<input type="checkbox" value="">
<!-- todo text -->
<span class="text">56 % | Set registeration</span>
<!-- Emphasis label -->
<small class="label label-primary" style="float: right;white-space: nowrap"><i class="fa fa-clock-o"></i> 2018-06-30</small>
</li>
<hr>
<li class="bg-aqua" id="new_p_task"><b>Private tasks</b></li>
</ul>
</div>
.js文件
var pdf = new jsPDF('p', 'pt', 'letter'),
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
source = $('#list-todo')[0],
// we support special element handlers. Register them with jQuery-style
// ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
// There is no support for any other type of selectors
// (class, of compound) at this time.
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true;
},
'.hide': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true;
}
},
margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, // y coord
{
'width': margins.width // max width of content on PDF
'elementHandlers': specialElementHandlers
},
function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
pdf.save('tasks.pdf');
},
margins
);
失踪:
如何使代码生成与样式相同的完整PDF格式使用jsPDF或任何其他?
发布于 2018-08-08 10:54:48
我发现这里有问题:
只需替换这一行:
source = $('#list-todo')[0], // [line 4 : .js file ]
通过这个:
source = $('#list-todo').html(),
https://stackoverflow.com/questions/-100002005
复制相似问题