How to create TCPDF HTML table with variable row paddings
Asked Answered
D

2

18

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

$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.

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

Declamation answered 27/1, 2013 at 14:13 Comment(2)
@Deadlock How could styling with CSS class work, when direct inline CSS isn't working? I tried putting style="padding:10px" both on <tr> and <td> and none of them worked.Declamation
There is no problem in your code, maybe TCPDF doesn't support this...Allegorical
J
15

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

<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 cellpadding for each row:

<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>
Jugendstil answered 1/4, 2017 at 7:0 Comment(0)
D
0

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

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

Then in the TD which needs the padding:

$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.

Daylong answered 4/7, 2018 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.