【问题标题】:Export HTML table to pdf using jspdf使用jspdf将HTML表格导出为pdf
【发布时间】:2014-05-26 23:34:44
【问题描述】:

我需要使用 jspdf 将 HTML 表格导出为 pdf 文件。我尝试了以下代码,但它在 pdf 文件中显示空白/空输出。对此的任何建议或示例代码都会有所帮助。 `

<script type="text/javascript">
    function demo1() {
        $(function () {
            var specialElementHandlers = {
                '#editor': function (element,renderer) {
                    return true;
                }
            };
         $('#cmd').click(function () {
                var doc = new jsPDF();
                doc.fromHTML($('#htmlTableId').html(), 15, 15, {
                    'width': 170,'elementHandlers': specialElementHandlers
                });
                doc.save('sample-file.pdf');
            });  
        }); 
    }
</script>
`

【问题讨论】:

    标签: jspdf


    【解决方案1】:

    这是工作示例:

    在头脑中

    <script type="text/javascript" src="jspdf.debug.js"></script>
    

    脚本:

    <script type="text/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: 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('Test.pdf');
                }
                , margins);
            }
        </script>
    

    和桌子:

    <div id="customers">
            <table id="tab_customers" class="table table-striped" >
                <colgroup>
                    <col width="20%">
                    <col width="20%">
                    <col width="20%">
                    <col width="20%">
                </colgroup>
                <thead>         
                    <tr class='warning'>
                        <th>Country</th>
                        <th>Population</th>
                        <th>Date</th>
                        <th>Age</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Chinna</td>
                        <td>1,363,480,000</td>
                        <td>March 24, 2014</td>
                        <td>19.1</td>
                    </tr>
                    <tr>
                        <td>India</td>
                        <td>1,241,900,000</td>
                        <td>March 24, 2014</td>
                        <td>17.4</td>
                    </tr>
                    <tr>
                        <td>United States</td>
                        <td>317,746,000</td>
                        <td>March 24, 2014</td>
                        <td>4.44</td>
                    </tr>
                    <tr>
                        <td>Indonesia</td>
                        <td>249,866,000</td>
                        <td>July 1, 2013</td>
                        <td>3.49</td>
                    </tr>
                    <tr>
                        <td>Brazil</td>
                        <td>201,032,714</td>
                        <td>July 1, 2013</td>
                        <td>2.81</td>
                    </tr>
                </tbody>
            </table> 
        </div>
    

    和按钮运行:

    <button onclick="javascript:demoFromHTML()">PDF</button>
    

    和在线工作示例:

    tabel to pdf jspdf

    或试试这个:HTML Table Export

    【讨论】:

    • 单元格中的内容溢出有什么办法可以防止这种情况发生吗?
    • 效果很好,除了没有显示里面的任何 HTML 标签。例如,如果我在一个单元格中有一个UL,它只是在不同的行上显示文本。如果我有一个带下划线或粗体的文本,它不会显示出来。
    • 如何在 pdf 导出的表格上应用 css
    • 我只是复制了函数并将其触发为 svg,它什么也没做。有什么我没有考虑到的吗? @szakalq
    • 在 Firefox 的 JS fiddle 中提醒我 ReferenceError: jsPDF is not defined
    【解决方案2】:

    您也可以使用jsPDF-AutoTable 插件。您可以查看使用以下代码的演示 here

    var doc = new jsPDF();
    doc.autoTable("#basic-table");
    doc.save("table.pdf");
    

    【讨论】:

    • 我正在尝试使用这个包,因为它看起来很有能力。但是,如何编写按钮来执行保存表格的功能?
    • 我恐怕在尝试访问该页面时遇到 404 错误
    • 我的错,请查看此链接 examples/simple.html
    • 谢谢。但是,我的表格是动态生成的,并且是 HTML 格式的(恐怕我没有 JS 的经验)
    • 尝试查看来自 html 的示例。如果你不能让它工作,请随时发布一个新的 stackoverflow 问题并在此处链接到它。
    【解决方案3】:

    使用get(0) 而不是html()。也就是说,替换

    doc.fromHTML($('#htmlTableId').html(), 15, 15, {
        'width': 170,'elementHandlers': specialElementHandlers
    });
    

    doc.fromHTML($('#htmlTableId').get(0), 15, 15, {
        'width': 170,'elementHandlers': specialElementHandlers
    });
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关 why 和/或 如何 此代码回答问题的额外上下文将显着改善其长期-期限价值。请edit你的答案添加一些解释。
    【解决方案4】:

    我们可以分离出需要转换成PDF的部分

    例如,如果表属于类“pdf-table-wrap

    之后,我们需要结合jsPDF

    调用html2canvas函数

    以下是示例代码

    var pdf = new jsPDF('p', 'pt', [580, 630]);
    html2canvas($(".pdf-table-wrap")[0], {
        onrendered: function(canvas) {
            document.body.appendChild(canvas);
            var ctx = canvas.getContext('2d');
            var imgData = canvas.toDataURL("image/png", 1.0);
            var width = canvas.width;
            var height = canvas.clientHeight;
            pdf.addImage(imgData, 'PNG', 20, 20, (width - 10), (height));
    
        }
    });
    setTimeout(function() {
        //jsPDF code to save file
        pdf.save('sample.pdf');
    }, 0);
    

    完整教程在这里提供http://freakyjolly.com/create-multipage-html-pdf-jspdf-html2canvas/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2018-01-07
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多