首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用与html源代码相同的jsPDF创建PDF?

如何使用与html源代码相同的jsPDF创建PDF?
EN

Stack Overflow用户
提问于 2018-08-08 01:17:26
回答 1查看 0关注 0票数 0

我尝试使用jsPDF从html源代码创建pdf。

消息来源如下:

但发现PDF的结果如下:

这是使用的代码

HTML源

代码语言:txt
复制
<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文件

代码语言:txt
复制
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
);

失踪:

  • 第一个标题“li”元素
  • 右跨
  • 字体-令人敬畏的跨度
  • 所有风格

如何使代码生成与样式相同的完整PDF格式使用jsPDF或任何其他?

EN

回答 1

Stack Overflow用户

发布于 2018-08-08 10:54:48

我发现这里有问题:

只需替换这一行:

代码语言:txt
复制
source = $('#list-todo')[0],     // [line 4 : .js file ]

通过这个:

代码语言:txt
复制
source = $('#list-todo').html(),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档