php tcpdf 没有头部,TCPDF - 设置头部Logo不显示问题

在生成的pdf文件的页眉设置logo有以下解决方案

重新定义TCPDF中的K_PATH_IMAGES常量(define('K_PATH_IMAGES', xxxx) , 需要定义在加载tcpdf.php 之前)

## 示例

$logo = "xxxx"; //相对地址

$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "标题", "标语", array(0,64,255), array(0,64,128));

$pdf->setFooterData(array(0,64,0), array(0,64,128));

若不定义常量,可以将logo图片移动到vendor/tecnickcom/examples/images下

## 示例

$logo = "xxxx"; //相对地址

$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "标题", "标语", array(0,64,255), array(0,64,128));

$pdf->setFooterData(array(0,64,0), array(0,64,128));

重写 public function Header() {} 方法

public function Header() {

$logo = xxxx;//绝对地址

$this->Image($logo, 5, 2, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);

$this->SetFont('stsongstdlight', '', 10);

$this->MultiCell(0, 15, $this->title, 0, "R", false, 1, 0, 5);

}

设置头部Logo源码分析

$this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);

根据源码分析,发现此处 会在配置的 $headerdata['logo'] 数据前面,自动加上 常量 K_PATH_IMAGES,所以才有了解决方案1,方案2

## 源码

public function Header() {

if ($this->header_xobjid === false) {

// start a new XObject Template

$this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);

$headerfont = $this->getHeaderFont();

$headerdata = $this->getHeaderData();

$this->y = $this->header_margin;

if ($this->rtl) {

$this->x = $this->w - $this->original_rMargin;

} else {

$this->x = $this->original_lMargin;

}

if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {

$imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES.$headerdata['logo']);

if (($imgtype == 'eps') OR ($imgtype == 'ai')) {

$this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);

} elseif ($imgtype == 'svg') {

$this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);

} else {

$this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);

}

$imgy = $this->getImageRBY();

} else {

$imgy = $this->y;

}

$cell_height = $this->getCellHeight($headerfont[2] / $this->k);

// set starting margin for text data cell

if ($this->getRTL()) {

$header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);

} else {

$header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);

}

$cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);

$this->SetTextColorArray($this->header_text_color);

// header title

$this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);

$this->SetX($header_x);

$this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);

// header string

$this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);

$this->SetX($header_x);

$this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);

// print an ending header line

$this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));

$this->SetY((2.835 / $this->k) + max($imgy, $this->y));

if ($this->rtl) {

$this->SetX($this->original_rMargin);

} else {

$this->SetX($this->original_lMargin);

}

$this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');

$this->endTemplate();

}

// print header template

$x = 0;

$dx = 0;

if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {

// adjust margins for booklet mode

$dx = ($this->original_lMargin - $this->original_rMargin);

}

if ($this->rtl) {

$x = $this->w + $dx;

} else {

$x = 0 + $dx;

}

$this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);

if ($this->header_xobj_autoreset) {

// reset header xobject template at each page

$this->header_xobjid = false;

}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值