【发布时间】:2011-08-16 08:23:52
【问题描述】:
我为使用 TCPDF 创建的 PDF 创建了一个自定义标题。现在我想在页眉底部添加一条穿过页面但不知道怎么做的蓝线(大约 2px 宽)?
【问题讨论】:
我为使用 TCPDF 创建的 PDF 创建了一个自定义标题。现在我想在页眉底部添加一条穿过页面但不知道怎么做的蓝线(大约 2px 宽)?
【问题讨论】:
在当前位置画一条水平黑线:
$style = ['width' => 0.2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => [0, 0, 0]];
$this->pdf->SetLineStyle($style);
$this->pdf->Line(PDF_MARGIN_LEFT, $this->pdf->getY(), $this->pdf->getPageWidth()-PDF_MARGIN_LEFT, $this->pdf->getY());
$this->pdf->Ln();
【讨论】:
只需添加一些 HTML :)
$html ='<hr>';
$pdf->writeHTML($html, true, false, true, false, '');
【讨论】:
我找到了最简单的放线方法
$pdf->writeHTML("<hr>", true, false, false, false, '');
【讨论】:
你也可以使用页轴:
$pdf->Line(5, $pdf->y, $pdf->w - 5, $pdf->y);
但是,如果您尝试呈现彩色的 <hr> html 标记,则需要调整 TCPDF::DrawColor(此摘录来自根据 $twidth 向数据报告的每一行添加图表栏的代码和$lengthmm):
$htmlbar = '<hr style="width:' . $lengthmm . 'mm;">';
$oldDrawColor = $pdf->DrawColor;
$pdf->setDrawColor(121, 161, 46);
$pdf->MultiCell($twidth,'2',$htmlbar,0,'L',$fill,1,'','',true,0,true,false,4,'T',false);
$pdf->DrawColor = $oldDrawColor;
【讨论】:
重点是获取第二个点的x值。 我就是这样做的:
$pageWidth = $pdf->getPageWidth(); // Get total page width, without margins
$pageMargins = $pdf->getMargins(); // Get all margins as array
$headerMargin = $pageMargins['header']; // Get the header margin
$px2 = $pageWidth - $headerMargin; // Compute x value for second point of line
$p1x = $this->getX();
$p1y = $this->getY();
$p2x = $px2;
$p2y = $p1y; // Use same y for a straight line
$style = array();
$this->Line($p1x, $p1y, $p2x, $p2y, $style);
链接
TCPDF::getMargins()
http://www.tcpdf.org/doc/code/classTCPDF.html#ae9bd660bf5b5e00eea82f1168cc67b5b
TCPPDF::getPageWidth()
http://www.tcpdf.org/doc/code/classTCPDF.html#a510ab21d6a373934bcd3bd4683704b7e
玩得开心!
【讨论】:
我相信你会这样做:
$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0));
$pdf->Line(5, 10, 80, 30, $style);
这是完整的例子
【讨论】: