【问题标题】:Align text by baseline in FPDF在 FPDF 中按基线对齐文本
【发布时间】:2013-08-11 03:35:32
【问题描述】:

我正在使用以下代码使用 FPDF 显示标题和日期:

$this->SetFont('Helvetica', 'B', 30);
$this->Cell(120, 20, 'Rechnung 20130809-78');

$this->SetFont('Helvetica', '', 10);
$this->Cell(0, 20, '09. 08. 2013');

但是文本没有正确对齐:

我怎样才能让它工作以使基线处于相同的高度?

我不想要一个必须手动调整其中一个元素的位置的解决方案。它必须适用于我输入的每种字体大小。

我已经尝试在我的 Cell 方法中自动调整 y 位置,但是文本没有在基线处对齐,而是在底部(g 结束的地方)!

public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='') {
    $text = utf8_decode($txt);

    $startX = $this->GetX();
    $startY = $this->GetY();

    $this->SetY($startY - $this->FontSize / 2);
    $this->SetX($startX);

    parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);

    $endX = $this->GetX();
    $endY = $this->GetY();

    $this->SetY($startY);
    $this->SetX($endX);
}

有什么方法可以做我想做的事吗?请帮我!上图中的绿线应该在同一高度。

【问题讨论】:

    标签: php pdf-generation fpdf typography baseline


    【解决方案1】:

    这里有一个解决方案:

    function drawTextBox($strText, $w, $h, $align='L', $valign='T', $border=true)
    {
            $xi=$this->GetX();
            $yi=$this->GetY();
    
            $hrow=$this->FontSize;
            $textrows=$this->drawRows($w,$hrow,$strText,0,$align,0,0,0);
            $maxrows=floor($h/$this->FontSize);
            $rows=min($textrows,$maxrows);
    
            $dy=0;
            if (strtoupper($valign)=='M')
                    $dy=($h-$rows*$this->FontSize)/2;
            if (strtoupper($valign)=='B')
                    $dy=$h-$rows*$this->FontSize;
    
            $this->SetY($yi+$dy);
            $this->SetX($xi);
    
            $this->drawRows($w,$hrow,$strText,0,$align,false,$rows,1);
    
            if ($border)
                    $this->Rect($xi,$yi,$w,$h);
    }
    

    来源:https://github.com/lsolesen/fpdf/blob/master/examples/textbox/textbox.php

    还有一个插件:http://fpdf.de/downloads/addons/52/

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 2011-11-09
      • 2015-07-27
      • 2011-09-07
      • 2019-10-03
      • 1970-01-01
      • 2021-11-29
      • 2015-12-03
      • 2014-12-04
      相关资源
      最近更新 更多