php - 如何根据 HTML 输入值在 TCPDF 中设置 CSS 样式?

标签 php html css tcpdf

我有这些 HTML 值,我设置了默认值“Check Weighted

<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 将下注设置为 css background-color green and red for “Check Weighted”

问题出现在 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, '');

关于php - 如何根据 HTML 输入值在 TCPDF 中设置 CSS 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54510012/

相关文章:

php - PHP 中的序列化和 utf-8

php - 如何在 PHP 中使用正则表达式查找完全匹配项?

PHP 尝试在 while 循环中向数组添加值

javascript - 保持风格的按钮

php - 如何通过 Cron 作业或脚本检查守护程序服务是否正在运行?

html - 抑制链接标题

javascript - 蓝色窗口在 3 秒后消失

html - 如何在 Angular/Ionic 框架中设置 ionic 输入的样式?

html - 如何使一个元素在另一个元素和容器边缘之间居中

html - 内联 block 元素换行时的CSS,父包装器不适合新宽度