7

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') ?
Dave
  • 3,073
  • 7
  • 20
  • 33
NORM
  • 235
  • 4
  • 7
  • 14

6 Answers6

16

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.

Ing. Michal Hudak
  • 5,338
  • 11
  • 60
  • 91
BlackMagic
  • 4,378
  • 1
  • 21
  • 14
6

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);
C. Jaacks
  • 74
  • 1
  • 3
4

This may work for you

MultiCell(0,$height,'You can<P ALIGN="center">center a line</P>',0,'C')
Balram Singh
  • 1,576
  • 19
  • 30
  • 3
    Welcome to Stack Overflow! Rather than only post a block of code, please explain why this code solves the problem posed. Without an explanation, this is not an answer. – Artemix Nov 22 '12 at 11:25
  • @Artemix is right, you should explain the code. Nevertheless, it worked perfect for me. – manuelmatas Sep 08 '15 at 00:58
2
$pdf->Text($mid_x-$pdf->GetStringWidth($text)/2,$y,$text);
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
taur
  • 1,031
  • 1
  • 7
  • 2
0
            global $title;

            
            // Calculate width of text and position
            $w = $this->GetStringWidth($title)+6;
            $this->SetX((210-$w)/2);
            
        
            // Line break
            $this->Ln(10);

         
            $title = 'YOUR TEXT HERE';
            $this->SetTitle($title);
            $this->SetTextColor(0,0, 0);
            $this->SetFont('Arial','',17);
            $this->SetX(((210-$w)/4),(-50));
            $this->Cell(10, 20, $title);
Prashanth Benny
  • 1,523
  • 21
  • 33
0

This worked for me

$mid_x = $pdf->GetPageWidth() / 2;
$text = $userName;
$pdf->Text($mid_x - ($pdf->GetStringWidth($text) / 2),{height}, $text);
Priyanshi
  • 1
  • 2