Reputation: 3537
I am using the following code to display a headline and a date next to each other using FPDF:
$this->SetFont('Helvetica', 'B', 30);
$this->Cell(120, 20, 'Rechnung 20130809-78');
$this->SetFont('Helvetica', '', 10);
$this->Cell(0, 20, '09. 08. 2013');
But the texts are not aligned properly:
How can I get it to work so that the baselines are on the same height?
I do not want a solution where I have to adjust the position of one of the elements manually. It has to work with every font-size I enter.
I already have tried to adjust the y-position automatically in my Cell-method, but the text is then not aligned at the baselines but at the bottom (where the g ends)!
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);
}
Is there any way to do what I intend to do? Please help me! The green lines in the image above should be at the same height.
Upvotes: 3
Views: 2421
Reputation: 1045
Here is a solution:
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);
}
source: https://github.com/lsolesen/fpdf/blob/master/examples/textbox/textbox.php
There is also an addon for that: http://fpdf.de/downloads/addons/52/
Upvotes: 1