Erick Sanabria
Erick Sanabria

Reputation: 1

How to center a text using coordinates and text length

I'm generating a certificate with a user name for a course platform. I want to center a text but it is not centered at all, It starts in the middle but the text is moved i. I'm using PHP 8.0 and the library MPDF. My code is the following:

$mpdf = new \Mpdf\Mpdf(['orientation' => 'L']);
$mpdf->setSourceFile('storage/' . $certificate->url); // absolute path to pdf file
$certificatePDf = $mpdf->importPage(1);

$mpdf->useTemplate($certificatePDf, 0, 0, null, null, true);
$pageSize = $mpdf->getTemplateSize($certificatePDf);
$textSize = strlen($name);
$mpdf->SetFont('Sanchez-Regular', '', 40,); 
$mpdf->SetY(130);
//centering the name on the pdf
$mpdf->WriteText($pageSize['width']/2 - $textSize/2  , 130, $name);
$mpdf->Output('newpdf.pdf');

My formula is the following: $totalPageSize/2 - $textSize/2. An example:formula result What else if left to center based on text length?

Upvotes: -1

Views: 139

Answers (1)

Erick Sanabria
Erick Sanabria

Reputation: 1

Mpdf allows to enter text in HTML, even if you're trying to load a pdf or an HTML view as a template.

Here is what I did: My Solution

Upvotes: 0

Related Questions