php 如何创建具有可变行填充的 TCPDF HTML 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14548225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:30:03  来源:igfitidea点击:

How to create TCPDF HTML table with variable row paddings

phpcssreporttcpdf

提问by Bero_zg

I'm trying to create an HTML table in TCPDF, with few rows having grater space between rows (padding), and others having smaller padding.

我正在尝试在 TCPDF 中创建一个 HTML 表,其中几行在行之间具有更大的空间(填充),而其他行具有更小的填充。

$html = '<table border="0" cellpadding="6">
        <tr>
            <td style="width="52%">' . lang('ticket_name') . '</td>
            <td style="width="18%">' . lang('ticket_price') . '</td>
            <td style="width="12%">' . lang('quantity') . '</td>
            <td style="width="18%">' . lang('total') . '</td>
        </tr>
    <tr>
            <td style="padding: 10px">' . $item['name'] . '</td>
            <td style="padding: 10px">' . $item['unit_price'] . '</td>
            <td style="padding: 10px">' . $item['quantity'] . '</td>
            <td style="padding: 10px">' . $item['row_total'] . '</td>
        </tr>
        <tr>
            <td style="text-align:right" colspan="3">' . lang('price_basis') . ': </td>
            <td>' . $totals['total_before_tax'] . '</td>
        </tr>
            <tr>
            <td style="text-align:right" colspan="3">' . 'Ukupni popust' . ': </td>
            <td>' . $totals['total_discount'] . '</td>
        </tr>
            <tr>
            <td style="text-align:right" colspan="3">' . 'Sveukupno' . ': </td>
            <td>' . $totals['grand_total'] . '</td>
        </tr>
    </table>';
$pdf->writeHTML($html, $linebreak = true, $fill = false, $reseth = true, $cell = false, $align = '');

As you can see, I have a cellpadding attribute in the table tag, which is working fine, but I want to have a different padding in the second row. Padding style obviously isn't working on 'td' nor on 'tr' tags.

如您所见,我在 table 标记中有一个 cellpadding 属性,它工作正常,但我想在第二行中有一个不同的填充。填充样式显然不适用于“td”或“tr”标签。

I know it can be done with two separate tables with different cellpaddings, but it seems pretty wrong. There must be another way.

我知道这可以用两个不同的单元格填充的单独表格来完成,但这似乎很错误。必须有另一种方式。

采纳答案by Alex

TCPDF doesn't support all CSS attributes, for example TCPDF doesn't support paddingand marginattributes. You can simulate paddings by inner table:

TCPDF 并不支持所有的 CSS 属性,例如 TCPDF 不支持paddingmargin属性。您可以通过内表模拟填充:

<tr>
 <td>
  <table><tr>
   <td style="width:10px;"></td>
   <td>' . $item['name'] . '</td>
   <td style="width:10px;"></td>
  </tr></table>
 </td>
</tr>

Or you can use different tables with different cellpaddingfor each row:

或者您可以使用不同的表,cellpadding每一行都不同:

<table border="0" cellpadding="6">
 <tr>
  <td>' . lang('ticket_name') . '</td>
  <td>' . lang('ticket_price') . '</td>
  <td>' . lang('quantity') . '</td>
  <td>' . lang('total') . '</td>
 </tr>
</table>
<table border="0" cellpadding="5">
 <tr>
  <td>' . $item['name'] . '</td>
  <td>' . $item['unit_price'] . '</td>
  <td>' . $item['quantity'] . '</td>
  <td>' . $item['row_total'] . '</td>
 </tr>
</table>

回答by Ukuser32

The other answer here provides for an all roundpadding however if you need padding at the top or bottom the solution is to use a transparent PNG with a set height/width.

此处的另一个答案提供了全方位填充,但是如果您需要在顶部或底部填充,则解决方案是使用具有设置高度/宽度的透明 PNG。

private function padding($width,$height){
    return '<img src="/imagepath/images/transparent.png" width="'.$width.'" height="'.$height.'">'; 
}

Then in the TD which needs the padding:

然后在需要填充的 TD 中:

$this->padding(82,11)

$this->padding(82,11)

So you need a defined width on the table cell. This might also work for left/right padding too but havent tried it.

所以你需要在表格单元格上定义一个宽度。这也可能适用于左/右填充,但还没有尝试过。