Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right justify header test using TCPDF

Tags:

tcpdf

This first image shows the standard header layout when using TCPDF:

enter image description here

What I would like to do is right justify the text, as shown below, but I have not been able to figure out how to do this:

enter image description here

Please offer some suggestions! Thanks.

like image 720
GRoston Avatar asked Sep 14 '25 00:09

GRoston


1 Answers

One way to accomplish this is with the writeHTMLCell() method. Setting the $w parameter to 0 will cause the cell to extend to the right margin. The $align parameter can then be set to 'R', which will right-align the cell content.

Example

$html = '<strong>Header Title</strong><br/>
         Header string, Line 1<br/>
         Header string, Line 2<br/>
         Header string, Line 3';
$pdf->writeHTMLCell(
    $w=0,
    $h=0,
    $x=0,
    $y=10,
    $html,
    $border=0,
    $ln=0,
    $fill=false,
    $reseth=true,
    $align='R'
);

Working Example

This full example can be run in the TCPDF examples directory.

<?php
require_once('tcpdf_include.php');
class MYPDF extends TCPDF
{
    public function Header()
    {
        $image_file = K_PATH_IMAGES.'logo_example.jpg';
        $this->Image($image_file, 10, 10, 15, '', 'JPG');
        $html = '<strong>Header Title</strong><br/>
                 Header string, Line 1<br/>
                 Header string, Line 2<br/>
                 Header string, Line 3';
        $this->writeHTMLCell(
            $w=0,
            $h=0,
            $x=0,
            $y=10,
            $html,
            $border=0,
            $ln=0,
            $fill=false,
            $reseth=true,
            $align='R'
        );
    }
}

$pdf = new MYPDF();
$pdf->AddPage();
$pdf->Output('example.pdf', 'I');
like image 72
Adam Moller Avatar answered Sep 17 '25 20:09

Adam Moller