如何使用与HTML源代码相同的jsPDF创建PDF

时间:2018-07-07 23:26:42

标签: jspdf

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

源看起来像这样: html list

但是找到这样生成的PDF: PDF created

这是使用的代码

HTML源

select
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name
from
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a
where
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relkind = 'r'
    and t.relname like 'test%'
order by
    t.relname,
    i.relname;

.js文件

<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>

丢失:

  • 第一个标题“ li”元素
  • 正确跨度
  • 超棒的字体
  • 输入元素
  • 所有样式

我如何使代码生成完整的PDF格式,与样式HTML相同(使用jsPDF或任何其他格式)

  

修改后:


我发现丢失文字的问题

只需替换此行:

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
);

通过这个:

source = $('#list-todo')[0],     // [line 4 : .js file ]

现在

文档样式仍然存在问题

PDF仍然显示为this

仍然丢失:

  • 超棒的字体
  • 输入元素
  • 所有样式

如何使样式和所有字体和字符correctly出现在PDF中


  

这也是我需要的this question答案

0 个答案:

没有答案