php - 在 FPDF 中按基线对齐文本

标签 php pdf-generation fpdf typography baseline

我使用以下代码通过 FPDF 显示彼此相邻的标题和日期:

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

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

但是文本没有正确对齐:

The date is to high.

我怎样才能使基线在同一高度上工作?

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

我已经尝试在我的 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);
}

They are aligned at the bottom instead of the baseline.

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

最佳答案

解决方法:

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/

关于php - 在 FPDF 中按基线对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18142691/

相关文章:

javascript - 将 $_GET [id] 传递给刷新

javascript - 使用 Canvas 数据的 PDFMake 循环不能保持正确的顺序

java - 使用java api从Word文档创建PDF文档

javascript - 根据 API 响应在组件内 React 渲染 PDF 文件

php - FPDF:决定​​何时设置页眉/页脚数据

php - 在 php echo 中重新加载页面不起作用

PHP5面向对象: Accessing changed parent properties

PHP PDO 转义问号所以它不认为它是一个占位符

python - 使用 fpdf 将 jpg 添加到 pdf

php - 如何使用 FPDF/FPDI 获取准确修改后的 PDF?