TCPDF 在单个 <hr> 标签上报告 "Undefined index: style"

TCPDF reports "Undefined index: style" on single <hr> tag

简单

$pdf->writeHTML("<hr>", true, false, false, false, '');

上次测试时确实有效(几个版本的 TCPDF 之前),现在导致“未定义的索引:样式”。

TCPDF 来源的相关代码是

case 'hr': {
    if ((isset($tag['height'])) AND ($tag['height'] != '')) {
        $hrHeight = $this->getHTMLUnitToUnits($tag['height'], 1, 'px');
    } else {
        $hrHeight = $this->GetLineWidth();
    }
    $this->addHTMLVertSpace($hbz, max($hb, ($hrHeight / 2)), $cell, $firsttag);
    $x = $this->GetX();
    $y = $this->GetY();
    $wtmp = $this->w - $this->lMargin - $this->rMargin;
    if ($cell) {
        $wtmp -= ($this->cell_padding['L'] + $this->cell_padding['R']);
    }
    if ((isset($tag['width'])) AND ($tag['width'] != '')) {
        $hrWidth = $this->getHTMLUnitToUnits($tag['width'], $wtmp, 'px');
    } else {
        $hrWidth = $wtmp;
    }
    $prevlinewidth = $this->GetLineWidth();

    $this->SetLineWidth($hrHeight);
        $lineStyle = array(
        'color' => $tag['fgcolor'],
        'cap'   => $tag['style']['cap'], // Error thrown here.
        'join'  => $tag['style']['join'],
        'dash'  => $tag['style']['dash'],
        'phase' => $tag['style']['phase'],
    );

    $lineStyle = array_filter($lineStyle);

    $this->Line($x, $y, $x + $hrWidth, $y, $lineStyle);
    $this->SetLineWidth($prevlinewidth);
    $this->addHTMLVertSpace(max($hbc, ($hrHeight / 2)), 0, $cell, !isset($dom[($key + 1)]));
    break;

$tag的值为

Array
(
    [elkey] => 0
    [tag] => 1
    [value] => hr
    [block] => 1
    [opening] => 1
    [parent] => 0
    [self] => 1
    [hide] => 
    [fontname] => dejavusans
    [fontstyle] => 
    [fontsize] => 10
    [font-stretch] => 100
    [letter-spacing] => 0
    [stroke] => 0
    [fill] => 1
    [clip] => 
    [line-height] => 1.25
    [bgcolor] => 
    [fgcolor] => Array
        (
            [R] => 0
            [G] => 0
            [B] => 0
        )

    [strokecolor] => Array
        (
            [R] => 0
            [G] => 0
            [B] => 0
        )

    [align] => 
    [listtype] => 
    [text-indent] => 0
    [text-transform] => 
    [border] => Array
        (
        )

    [dir] => ltr
    [attribute] => Array
        (
        )

)

可以看出,没有style键。我做错了什么,还是 TCPDF 中的错误?我该如何解决?

乍一看,这可能确实是 TCPDF 中的一个错误。看看他们如何 运行 和 array_filter() 之后过滤掉空值。此类“未定义索引”错误会触发 E_notice 错误,这些错误在许多 PHP 配置中都被遗漏了。

正如 TRiG 所说,这是 TCPDF 中的一个错误。如果您查看代码,您会看到 TCPDF 使用 isset 检查他的参数是否存在。在 hr 的情况下,isset 测试缺少样式(查看版本 6.4.1 的第 18902 行,您会看到这样的高度测试) 为避免此问题,您有 4 个选项:

  1. 使用无法设置样式的旧 TCPDF 版本 hr

  2. 等待新版本更新

  3. 在代码中添加一个isset测试有点像这样:

             // Line 18922
             $a = isset($tag['style']['cap']);
             if ($a== false)
             {
                 // Add here the default value you want
                 $tag['style']['cap'] = "";
                 $tag['style']['join'] = "";
                 $tag['style']['dash'] = "";
                 $tag['style']['phase'] = "";
             }
             // The TCPDF code, using the values
             $lineStyle = array(
                 'color' => $tag['fgcolor'],
                 'cap'   => $tag['style']['cap'],
                 'join'  => $tag['style']['join'],
                 'dash'  => $tag['style']['dash'],
                 'phase' => $tag['style']['phase'],
             );
    
  4. 在 HTML 的底部添加 hr 的默认样式(简单的解决方案!)