首页 > 解决方案 > 数据表 PDFExport 将行内容垂直居中对齐

问题描述

我有一个导出 pdf 文档的数据表,如下所示: 导出的 pdf 文本内容垂直顶部对齐

如何使行内的文本垂直居中对齐,如下所示? 导出的 pdf 文本内容垂直居中对齐

这是我的示例代码:

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.css"/>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.6.5/css/buttons.dataTables.css"/>
  </head>
  <body>
    <table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start<br />date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger<br />Nixon</td>
                <td>System<br />Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett<br />Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton<br />Cox</td>
                <td>Junior<br />Technical<br />Author</td>
                <td>San<br />Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
            <tr>
                <td>Cedric<br />Kelly</td>
                <td>Senior<br />Javascript<br />Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>$433,060</td>
            </tr>
            <tr>
                <td>Airi<br />Satou</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>33</td>
                <td>2008/11/28</td>
                <td>$162,700</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start<br />date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
    
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.js"></script>
    <script>
      $(document).ready(function() {
          $('#example').DataTable({
            dom: "Bftrip",
            buttons: [
                {
                  extend: 'pdfHtml5',
                  footer: true,
                  exportOptions: {
                    format: {
                      header: function ( data, row, column, node ) {
                        return data.replace(/<br\s*\/?>/ig, "\r\n");
                      },
                      body: function ( data, row, column, node ) {
                        return data.replace(/<br\s*\/?>/ig, "\r\n");
                      },
                      footer: function ( data, row, column, node ) {
                        return data.replace(/<br\s*\/?>/ig, "\r\n");
                      }
                    },
                  },
                  customize: function(doc) {
                    doc.pageMargins = [50, 50, 50, 50];
                    doc.content.splice(0,1);
                    doc.content[0].table.widths = [80, 80, 80, 50, 80, 80];
                    
                    var rowCount = document.getElementById('example').rows.length-1;
                    for(var a=1; a<rowCount; a++) {
                      doc.content[0].table.body[a][0].alignment = 'left';
                      doc.content[0].table.body[a][1].alignment = 'center';
                      doc.content[0].table.body[a][2].alignment = 'center';
                      doc.content[0].table.body[a][3].alignment = 'center';
                      doc.content[0].table.body[a][4].alignment = 'center';
                      doc.content[0].table.body[a][5].alignment = 'right';
                    }
                    var objLayout = {};
                    objLayout['hLineWidth'] = function(i) { return .5; };
                    objLayout['vLineWidth'] = function(i) { return .5; };
                    objLayout['hLineColor'] = function(i) { return '#aaa'; };
                    objLayout['vLineColor'] = function(i) { return '#aaa'; };
                    objLayout['paddingLeft'] = function(i) { return 2; };
                    objLayout['paddingRight'] = function(i) { return 2; };
                    doc.content[0].layout = objLayout;
                  }
                }
            ]
          });
      });
    </script>
  </body>
</html>

标签: javascriptjquerydatatabledatatablespdfmake

解决方案


推荐阅读