Reputation: 151
I am trying to generate pdf with database values using tcpdf.
I have a function to fetch data like this
function fetch_data()
{
$output = '';
include('../connection.php');
// Colors, line width and bold font
//My sql
$sql ="";
$query = mysqli_query($con, $sql) or die (mysqli_error($con));
$total_amt = 0;
$tax_amt = 0;
while($row = mysqli_fetch_array($query)) {
$output .= '<tr>
<td style="border-bottom:1px thin #666; font-size:12px; font-weight:normal; font-style:normal;">'.$row["product"].' - '.$row['description'].'</td>
<td style="border-bottom:1px thin #666"; align="center">'.$row["uom"].'</td>
<td style="border-bottom:1px thin #666"; align="center">'.$row["price"].'</td>
<td style="border-bottom:1px thin #666"; align="center">'.$row["qty"].'</td>
<td style="border-bottom:1px thin #666"; align="center">'.$row["discount"].'</td>
<td style="border-bottom:1px thin #666"; align="center">'.$row["taxation"].'%</td>
<td style="border-bottom:1px thin #666"; align="center">'.$row["total"].'</td>
</tr>
';
$total_amt += $row['total'];
$tax_amt += $row['tax_amount'];
}
$output .= '<tr><td colspan="7" style="border-top:1px thin #666";></td></tr><tr><td colspan="5" align="left"><strong>Total</strong></td><td align="center">'.number_format($tax_amt,2).'</td><td align="center">'.number_format($total_amt,2).'</td></tr>';
return $output;
}
And i display it
$content = '';
$content .= '
<table border="1"><tr><td width="100%">
<table border="0">
<tr bgcolor="#796799" style="color:#FFF;">' ;
$pdf->SetFont('arsenalb', 'B', 12);
$content .= '
<th width="40%" style="border-left-width:14px;border-left-color:#796799;" height="30" align="center">Description</th>
<th width="10%" align="center">UOM</th>
<th width="13%" align="center">Price</th>
<th width="7%" align="center">Qty</th>
<th width="7%" align="center">Disc</th>
<th width="10%" align="center">Tax</th>
<th width="13%" align="center">Total</th>
</tr>
';
$pdf->SetFont('arsenal', '', 10);
$content .= fetch_data();
$content .= '</table></td></tr></table>';
$pdf->writeHTML($content);
I want only the table headers to be bold, so i have used $pdf->SetFont('arsenal', '', 10);
after the header . But it is making the header also normal, not bold. if i remove this, then whole table data is becoming bold.
is there any way to separate the table something like this? https://tcpdf.org/examples/example_011/
Upvotes: 1
Views: 2913
Reputation: 1804
The reason this wont work is quite simpel. You write all $content to the $pdf AFTER the second SetFont line. Meaning that the first line is used, but since no content is added to the pdf, it wont show anything.
Since tcpdf allows you to write HTML, wyy not use HTML to make the header bold? with the <b>
tag. Another option is that you make two tables. One for the header and one for the content. Since you define the width of the columns, you can do this.
You'll get something like: $hdr = 'full table with header data';
$pdf->SetFont('arsenalb', 'B', 12);
$pdf->writeHTML($hdr);
$content .= 'new table, with column with defined';
$content .= fetch_data();
$content .= '</table></td></tr></table>';
$pdf->SetFont('arsenal', '', 10);
$pdf->writeHTML($content);
but that might work depending on if you need that second outer table. If you need it, it will most likely not possilbe, unless you can write partial HTML yo $pdf->writeHTML(). I'm not sure you can/cannot.
Upvotes: 1