Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Center Text in FPDF?

How can I have this generated text appear centered in the page.

Generated = $_POST method ... so I don't know how long will the text in input be. I need to have a pre-determined center parameter somehow.

Any ideas? Maybe like this:

MultiCell(0,$height,"text",0,'C') ?
like image 578
NORM Avatar asked Jan 20 '11 01:01

NORM


People also ask

How do you justify text in FPDF?

1 : -1); $style=''; foreach(array('B','I','U') as $s) if($this->$s>0) $style. =$s; $this->SetFont('',$style); } function PutLink($URL,$txt) { //Put a hyperlink $this->SetTextColor(0,0,255); $this->SetStyle('U',true); $this->Write(5,$txt,$URL); $this->SetStyle('U',false); $this->SetTextColor(0); } } ?>

What is MultiCell in FPDF?

The MultiCell function facilitates the generation of complex table structures within the FPDF PDF generating library. When used properly, the application can handle variable length data within each cell of the table. Simplistically stated, the MultiCell function is all about geometries.

How do you use FPDF?

The PDFlib needs to be installed as an extension in your PHP package, whereas FPDF can just be included in your PHP script and it's ready to use. To get started, you will need to download the FPDF class from the FPDF Web site and include it in your PHP script like this: require('fpdf.

How do you wrap text in FPDF cell?

FreeKB - PHP Wrap text in a cell when using FPDF. Create a class to extend FPDF with the vcell function. Use the WrapText class. $pdf = new WrapText();


2 Answers

Normally it's $pdf->Cell(0, $height, "text", 0, 0, 'C'); but if you're doing it in a Header or Footer function it's $this->Cell(0, $height, "text", 0, 0, 'C'). Don't forget to declare $height as a global if you're doing this in a function() call.

like image 73
BlackMagic Avatar answered Oct 17 '22 16:10

BlackMagic


Thanks taur! This works for me:

$mid_x = 135; // the middle of the "PDF screen", fixed by now.
$text = $userFullName;
$pdf_file->Text($mid_x - ($pdf_file->GetStringWidth($text) / 2), 102, $text);
like image 25
C. Jaacks Avatar answered Oct 17 '22 15:10

C. Jaacks