微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TCPDF 在单个 <hr> 标签上报告“未定义索引:样式”

如何解决TCPDF 在单个 <hr> 标签上报告“未定义索引:样式”

简单的

$pdf->writeHTML("<hr>",true,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,$linestyle);
    $this->Setlinewidth($prevlinewidth);
    $this->addHTMLVertSpace(max($hbc,!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 添加默认样式(简单的解决方案!)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。