php - 如何根据 HTML 输入值在 TCPDF 中设置 CSS 样式?
问题描述
我有这些 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 ”或“ Check Weighted ”。如果“OK”,css 将设置为“Check Weighted”的 css 背景色绿色和红色
问题出现在 TCPDF 中,我需要根据上面的 HTML 值设置 css 样式背景颜色。
我尝试了几种方法,但 css 样式背景颜色没有加载到 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');
}
我错过了什么吗?
感谢有人可以帮助我解决这个问题。
谢谢。
解决方案
尝试这个。if 条件内部使用字符串连接($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, '');
推荐阅读
- python - 如何从数据框python中的url检索.txt文件内容
- javascript - 我想通过数组中的重复数据对数据进行排序[Vue中的Javascript]
- elasticsearch - 修改 logstash 配置后确实创建了所需的字段
- ios - Peer Unity IOS 重置连接
- performance-testing - 火炮不发射场景
- git - Git 远程拉取不进行
- android - 启动后使用广播接收器启动应用程序在android 10中不起作用
- html - 网页上除了 CSS Wave Animation 什么都看不到
- php - SQLSTATE [42S22]:找不到列:1054 未知列“名称”,即使它存在于我的数据库表中
- html - 跨越标签在一起而不将文本分成两行