简体   繁体   English

如何使用jspdf.js正确打印HTML表格?

[英]How to print properly HTML table do PDF using jspdf.js?

here is what i am trying to achieve, i trying to print a html table that included HTML table inside too loop my detail (i have header and detail) data. 这是我想要实现的目标,我试图打印一个包含HTML表的html表,这也循环了我的详细信息(我有标题和详细信息)数据。 But when i use jspdf.js to print my HTML table, the table in the pdf is broken, is not look like the HTML, the looping table inside the main table is messy, look like it won't create the insider table. 但是当我使用jspdf.js打印我的HTML表时,pdf中的表坏了,看起来不像HTML,主表中的循环表很乱,看起来它不会创建内部表。 How to print the table inside table properly? 如何正确打印表格内的表格?

here is my HTML look like 这是我的HTML外观

index.html 的index.html

    <div id="customers">
    <div class="table-responsive">
        <table id="tbl" class="table table-hover">
            <thead>
                <tr>
                    <th style="background-color: #928989; color; white;">No BPS</th>
                    <th style="background-color: #928989; color; white;">Tanggal BPS</th>
                    <th style="background-color: #928989; color; white;">Tanggal Input</th>
                    <th style="background-color: #928989; color; white;">Status</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat-start="listLaporanRetur in listLaporanReturs | limitTo:quantity">
                    <td class="btn btn-mini btn-primary pull-center">BPXXXXXXX</td>
                    <td>2016-06-22</td>
                    <td>2016-06-22</td>
                    <td>SENT</td>
                </tr>
                <tr ng-repeat-end="">
                <td colspan="10" style="padding: 0">
                  <div>
                    <table class="table table-bordered table-hover">
                        <tr>
                            <td style="background-color: #80A3C1;">Kode Barang</td>
                            <td style="background-color: #80A3C1;">Qty</td>
                            <td style="background-color: #80A3C1;">Merk</td>
                            <td style="background-color: #80A3C1;">Hasil</td>
                        </tr> 
                        <tr ng-repeat-start="details in listLaporanRetur.returDetailList">
                            <td>STUFFID1</td>
                            <td>10</td>
                            <td>APPLE</td>
                            <td>BOOM</td>
                        </tr>
                  <tr>
                    <td>STUFFID2</td>
                            <td>40</td>
                            <td>SONY</td>
                            <td>BREAK</td>
                  </tr>
                        <tr ng-repeat-end=""></tr>
                    </table>

                  </div>
                </td>

                </tr>
            </tbody>
        </table>
    </div>
</div>
<button onclick="javascript:demoFromHTML();">PDF</button>

then here is my javascript 这是我的javascript

function demoFromHTML() {
    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 = $('#customers')[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
        }
    };
    margins = {
        top: 80,
        bottom: 60,
        left: 10,
        width: 700
    };
    // 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('Test.pdf');
    }, margins);
}

here is my jsfiddle http://jsfiddle.net/ugD5L/126/ 这是我的jsfiddle http://jsfiddle.net/ugD5L/126/

how to print the table properly so i don't have to get messy table in the pdf? 如何正确打印表格,这样我就不必在pdf中弄乱表格了? it's better if you guys can show me with jsfiddle example 如果你们能用jsfiddle示例向我展示会更好

As Mentioned in the one of answer nested table is not supported JsPDF so far, but if you can modify your html bit (if possible) then you can resolve your issue. 正如到目前为止,答案嵌套表之一中所提到的JsPDF尚不支持,但是如果您可以修改html位(如果可能),则可以解决问题。

    <tr ng-repeat-end="" class="table table-bordered table-hover">

                        <td style="background-color: #80A3C1;">Kode Barang</td>
                        <td style="background-color: #80A3C1;">Qty</td>
                        <td style="background-color: #80A3C1;">Merk</td>
                        <td style="background-color: #80A3C1;">Hasil</td>
                    </tr> 

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM