html - 使用文本对齐将 HTML 表格导出为 PDF

标签 html css pdf jspdf

我想知道是否有一种方法可以使用 jsPDF 导出文本的对齐方式。在导出 excel 文件时更容易,因为它接受表格的内联 css,但在将表格导出为 pdf 时,对齐方式都在左侧。这是我的脚本:

    <script>
    function demoFromHTML() {
        $(document).find('tfoot').remove();

        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 = $('#table')[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: 85,
            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
            var name = document.getElementById("name").innerHTML;
            pdf.save(name);
        }, margins);
        setTimeout("window.location.reload()",0.0000001);

    }
    </script>

我的表是这样的:

 <div id="table">
    <table data-filter="#filter" class="footable" style="background-color:white;">
        <thead>
            <tr>
                <th>Employee ID</th>
                <th>Branch Code</th>
                <th>Client ID</th>
                <th>Amount</th>
                <th>Report Timestamp</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            if(count($result)==0)
            { echo "<tr><td colspan='5' style='text-align:center; font-weight:bold;'>No data available</td></tr>"; }
            else
            {
            for($i=0; $i<count($result); $i++){?>
            <tr>
                <td align="left">1</td>
                <td align="left">2</td>
                <td align="left">3</td>
                <td align="right">4</td>
                <td align="left">5</td>
            </tr>

            <?php }
            }
            ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="8">
                    <div class="pagination pagination-centered"></div>
                </td>
            </tr>
        </tfoot>
    </table>
    </div>

最佳答案

目前 jsPDF 默认不支持对齐表格的单元格。相反,您可以使用第三方库(例如 HTMLTable[1])来执行此操作。

[1] : https://github.com/ArtBIT/jsPDF/blob/master/jspdf.plugin.htmltable.js

关于html - 使用文本对齐将 HTML 表格导出为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25988750/

相关文章:

javascript - 我的自动表单不会输出正确的值

css - 当外部 div 增长时,使内部 div 增长高度

jquery - 表格行点击,外部加载

javascript - 如何使用 jsPDF-AutoTable 插件自定义 PDF 中的标题单元格?

python - 使用 matplotlib 将图形保存到文件时为空 pdf 文件

PHP 隐藏/显示带分页的图像

javascript - 具有点击滚动功能的粘性导航

php - 如何创建选项包含背景图像和文本的下拉列表

javascript - <button> 在 HTML 中单击时改变颜色?

macos - 在 AppleScript 中从 PDF 文档到 JPEG 图像