我有这些HTML值,我设置默认值“检查加权”
<input type="hidden" id="total_weight" name="total_weight" value="Check Weighted" readonly="readonly">
<button type="submit" name="generate_pdf">Generate</button>
以上HTML值可以更改为“确定”或“检查加权”。如果“OK”,则css将下注设置为css background-color green和red为“Check Weighted”
问题来自TCPDF,我需要根据上面的HTML值设置css样式背景颜色。
我尝试了几种方法,但css样式background-color没有加载到TCPDF中。没有显示颜色。
我的代码如下:
if(isset($_POST['generate_pdf']))
{
$total_weight = $_POST['total_weight'];
if($total_weight == "Check Weighted")
{
$total_weight = "<td style='background-color:#red;'>Checked Weight</td>";
}
else if ($total_weight == "OK")
{
$total_weight = "<td style='background-color:#green;'>OK</td>";
}
// INITIALIZE
// create new PDF document
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Susan');
$pdf->SetTitle('Title');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Title');
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', 'B', 12);
// add a page
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 6);
// INITIALIZE
$tbl = <<<EOD
<table border="0">
<tr>
<th style="font-size:10px"><strong><u>TOTAL WEIGHT</u></strong></th>
</tr>
</table>
<table border="1" style="padding:5px 0 5px 5px">
<thead>
<tr nobr="true">
<th>Total Weight</th>
</tr>
</thead>
<tbody>
<tr nobr="true">
$total_weight
</tr>
</tbody>
</table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');
$pdf->Output('Title', 'I');
}
我试图在表格中添加if语句,但也没有用。
代码:
if(isset($_POST['generate_pdf']))
{
$total_weight = $_POST['total_weight'];
// INITIALIZE TCPDF
$tbl = <<<EOD
<table border="0">
<tr>
<th style="font-size:10px"><strong><u>TOTAL WEIGHT</u></strong></th>
</tr>
</table>
<table border="1" style="padding:5px 0 5px 5px">
<thead>
<tr nobr="true">
<th>Total Weight</th>
</tr>
</thead>
<tbody>
<tr nobr="true">
EOD;
if($total_weight == "Check Weighted")
{
$tbl = <<<EOD
<td style="background-color:#red;">Check Weighted</td>
EOD;
}
else if ($total_weight == "OK")
{
$tbl = <<<EOD
<td style="background-color:#green;">OK</td>
EOD;
}
$tbl = <<<EOD
</tr>
</tbody>
</table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');
$pdf->Output('Title', 'I');
}
我错过了什么吗?
感谢有人可以帮我解决这个问题。
谢谢。
尝试这个。如果条件使用字符串连接($ tbl。='';或$ tbl = $ tbl。'';),否则会抛出错误。
$total_weight = 'OK';
$tbl = '<table border="0">
<tr>
<th style="font-size:10px"><strong><u>TOTAL WEIGHT</u></strong>
</th>
</tr>
</table>
<table border="1" style="padding:5px 0 5px 5px">
<thead>
<tr nobr="true">
<th>Total Weight</th>
</tr>
</thead>
<tbody>
<tr nobr="true">';
if($total_weight == "Check Weighted") {
$tbl .= '<td style="background-color:red;">Check Weighted</td>';
}
else if ($total_weight == "OK") {
$tbl .= '<td style="background-color:green;">OK</td>';
}
$tbl .= '</tr>
</tbody>
</table>';
$pdf->writeHTML($tbl, true, false, false, false, '');