【问题标题】:TCPDF table header automatically move to the rightTCPDF 表头自动向右移动
【发布时间】:2019-04-12 23:39:41
【问题描述】:

我用 TCPDF 创建了一个表。具有黑色背景的标头已稍微向右定位,就好像左侧有一些填充一样。我无法让它正确排列。

下面的代码是我为形成表格而编写的 HTML。

$tbl ='<style>
th {background-color: black;color: white;float:left;}
.tal {text-align: left;float:left;}
</style>

<table border="0" cellspacing="0" cellpadding="0">
    <tr>
        <th width="60px" style="border-right: 1px solid white;"><strong>Qty</strong></th>
        <th width="1px"></th>
        <th class="tal" width="388px" style="padding:10px 0; border-right: 1px solid white;">     <strong>Product or Service</strong></th>
        <th width="1px"></th>
        <th width="84px" style="border-right: 1px solid white;"><strong>Price Each</strong></th>
        <th width="1px"></th>
        <th width="84px"><strong>Total</strong></th>
    </tr>
    
    <tr>
        <td height="267px">';
            while($i <= $a) {
                $tbl .= '<table height="267px" width="60px"><tr><td>' . $productsArray['product_quantity'][$i] . '</td></tr></table>';
                $i++;
            }
            $tbl .= '</td><td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td><td height="267px">';
            while($j <= $a) {
                $tbl .= '<table height="267px" width="388px"><tr><td class="tal">     ' . $productsArray['product_name'][$j] . '</td></tr></table>';
                $j++;
            }
            $tbl .= '</td><td width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td><td height="267px">';
            while($k <= $a) {
                $tbl .= '<table height="267px" width="84px"><tr><td>' . $productsArray['product_price'][$k] . '</td></tr></table>';
                $k++;
            }
            $tbl .= '</td><td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td><td height="267px">';
            while($l <= $a) {
                $tbl .= '<table height="267px" width="84px"><tr><td>' . $productsArray['product_sub'][$l] . '</td></tr></table>';
                $l++;
            }
$tbl .= '</td>
    </tr>
</table>';
}

下面的代码是我用来在页面上显示表格的PHP。

$pdf->writeHTMLCell(175, 80, 20, 100, $tbl, 1, 1, 0, true, 'C', true);

【问题讨论】:

    标签: php html css tcpdf


    【解决方案1】:

    在另一个表格的单元格中使用表格通常是一个非常糟糕的主意,并且可能会导致您遇到的问题。由于您的 productsArray 看起来已经拥有您需要的所有数据,因此我将简单地循环遍历它并在您执行时输出每一行。

    还值得指出的是,定义为宽度为 1px 的空标题行与定义为宽度为 0.5px 的实际数据行冲突。

    <style>
    th {background-color: black;color: white;float:left;}
    .tal {text-align: left;float:left;}
    </style>
    
    <table border="0" cellspacing="0" cellpadding="0">
        <tr>
            <th width="60px" style="border-right: 1px solid white;"><strong>Qty</strong></th>
            <th width="1px"></th>
            <th class="tal" width="388px" style="padding:10px 0; border-right: 1px solid white;">     <strong>Product or Service</strong></th>
            <th width="1px"></th>
            <th width="84px" style="border-right: 1px solid white;"><strong>Price Each</strong></th>
            <th width="1px"></th>
            <th width="84px"><strong>Total</strong></th>
        </tr>
    
    <?php
    while($i <= $a) {
    ?>
    <tr>
        <td height="267px"><?php echo $productsArray['product_quantity'][$i]; ?></td>
        <td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td>
        <td height="267px"><?php echo $productsArray['product_name'][$i]; ?></td>
        <td width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td>
        <td height="267px"><?php echo $productsArray['product_price'][$i]; ?></td>
        <td border="1" width="0.5px" height="267px" style="background:url(images/bars-black.jpg) bottom right no-repeat"></td>
        <td height="267px"><?php echo $productsArray['product_sub'][$l]; ?></td>
    </tr>
    <?php
        $i++;
    }
    ?>
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 2016-09-30
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多