Benny
Benny

Reputation: 1465

TCPDF Align Left Center Right in single line

I want to create a footer for a PDF document that contains the date left aligned, the creator centered and the page right aligned. These should be in a single line. I tried the following code:

$this->Cell(0, 10, $date->format('d.m.Y'), 0, false, 'L', 0, '', 0, false, 'T', 'M');
$this->Cell(0, 10, 'Creator', 0, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');

The creator is shifted to the right and overlays with the pages:

PDF Document Footer

Does anybody have a solution for that problem?

Upvotes: 6

Views: 43908

Answers (2)

Dean Sha
Dean Sha

Reputation: 845

I ran into alignment issue with TCPDF too. I noticed that if you use x-coordinate as 0, then it will use the attribute 'R' for right alignment. But if it's set to a non zero value then it ignores the 'R' setting. Here's the statement I used for right alignment.

$this->Cell(0, 9, 'Text-to-be-aligned-right', 0, false, 'R', 0, '', 0, false, 'T', 'M' );

Upvotes: 7

Sean
Sean

Reputation: 12433

You need to set the width of the Cell(), as according to the docs http://www.tcpdf.org/doc/code/classTCPDF.html#a33b265e5eb3e4d1d4fedfe29f8166f31 your $date->format('d.m.Y') Cell() is extending to the right margin, forcing the other cells on the line to the right margin.

$w (float) Cell width. If 0, the cell extends up to the right margin.

Try something like (may have to adjust based on font size)

$this->Cell(20, 10, $date->format('d.m.Y'), 0, false, 'L', 0, '', 0, false, 'T', 'M');   
$this->Cell(20, 10, 'Creator', 0, false, 'C', 0, '', 0, false, 'T', 'M'); 
$this->Cell(20, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');

Upvotes: 7

Related Questions