2012-10-25 176 views
4

我需要在pdf文件創建文本對齊(頂部和底部)。pdf對齊頂部和底部

我用這行:

$pdf->Cell(0,0,"Submitted To:$subto",0,0,'alignment area'); 

alignment area: L,R,C 

回答

2

我發現,似乎做你所需要的插件。在這裏,你有:http://fpdf.de/downloads/addons/52/

該插件提供的功能

drawTextBox(string strText, float w, float h [, string align [, string valign [, int border]]]) 

,其允許由所述函數的參數定義的矩形之內的文本對齊(水平和垂直)。

0

使用TCell將文本和BCell對齊到下面的文本行。

這對我很好,我希望它對你也有幫助。

class TBFpdf extends FPDF{ 

    private $padding_top = 1, $padding_bottom = 1, $line_space = 1, $paragraph_space = 1; 

    private function topBottom($type, array $args){ 
     if(!isset($args[0], $args[1], $args[2])) 
      throw new Exception("Parameter error!"); 

     $height = $args[1]; 
     $width = $args[0]; 

     $y_old = $this->GetY(); 
     $x_old = $this->GetX(); 

     if($type == "T"){ 
      $this->SetXY($x_old, $y_old + $this->padding_top); 
      $this->WriteLimit($width, $args[2], isset($args[5])? $args[5] : "L"); 
     }else{ 
      $text_height = $this->WriteLimit($width, $args[2], isset($args[5])? $args[5] : "L", true); 
      $this->SetXY($x_old, $y_old + $height - $text_height - $this->padding_bottom + 2); 
      $this->WriteLimit($width, $args[2], isset($args[5])? $args[5] : "L"); 
     } 
     //var_dump($height); 
     $this->SetXY($x_old, $y_old); 

     $args[2] = "";//empty the text already written 
     call_user_func_array([$this, 'Cell'], $args); 
    } 

    //the parameters are the same used by Cell 
    public function TCell(){ 
     call_user_func_array([$this, 'topBottom'], ['T', func_get_args()]); 
    } 

    //the parameters are the same used by Cell 
    public function BCell(){ 
     call_user_func_array([$this, 'topBottom'], ['B', func_get_args()]); 
    } 

    private function WriteLimit($width, $text, $align, $simulacao = false){ 
     $texts = explode(chr(13).chr(10), $text); 
     $height = 0; 

     for ($i = 0; $i < count($texts); $i++) { 

      $words = explode(' ', $texts[$i]); 
      $line = ""; 
      $qtd_words = count($words); 

      for ($j = 0; $j < $qtd_words; $j++) { 
       $new_word = $words[$j].' '; 
       $line .= $new_word; 

       if(($line_width = $this->getStringWidth($line)) > $width){ 
        $y_old = $this->GetY(); 
        $x_old = $this->GetX(); 
        $write_line = substr($line, 0, (-1)*strlen($new_word)); 
        $line = $words[$j].' '; 

        if(!$simulacao) 
         $this->AlignWrite($write_line, $width, $align); 

        $this->SetXY($x_old, $y_old + $this->FontSize + $this->line_space);//reset to the cell point 
        $height += $this->FontSize + $this->line_space; 
       } 

       if($j == $qtd_words-1){ 
        if(!$simulacao) 
         $this->AlignWrite($line, $width, $align); 
       } 
      } 

      $y_old = $this->GetY(); 
      $this->SetXY($x_old, $y_old + $this->FontSize + $this->paragraph_space);//reset to the cell point 
      $height += $this->FontSize + $this->paragraph_space; 

     } 

     return $height; 
    } 

    private function AlignWrite($write_line, $width, $align){ 
     $x_old = $this->GetX(); 

     if($align == "C" || $align == "R"){ 
      $width_write_line = $this->getStringWidth($write_line); 
      if($align == "C") 
       $this->SetX($x_old + ($width - $width_write_line)/2 - 0.75); 
      else 
       $this->SetX($x_old + $width - $width_write_line - 1.5); 
     } 

     $this->Write($this->FontSize, $write_line); 
    } 

    public function SetLineSpace($line_space){ 
     $this->line_space = (float)$line_space; 
     return $this; 
    } 

    public function SetParagraphSpace($paragraph_space){ 
     $this->paragraph_space = (float)$paragraph_space; 
     return $this; 
    } 

    public function SetPaddingTop($padding_top){ 
     $this->padding_top = (float)$padding_top; 
     return $this; 
    } 

    public function SetPaddingBottom($padding_bottom){ 
     $this->padding_bottom = $padding_bottom; 
     return $this; 
    } 

} 

如何使用:

$pdf = new TBFpdf(); 
$pdf->SetLineSpace(2)->SetPaddingTop(2)->SetParagraphSpace(3)->SetPaddingBottom(2); 
$pdf->AddPage(); 

$pdf->SetY(30); 
$pdf->SetX(15); 
$pdf->SetMargins(0, 0, 0); 
$pdf->SetFont('Arial','B', 20); 
$pdf->TCell(60, 210, utf8_decode(" Não se trata de um exercício intelectual, como aprender álgebra ou a análise de um balanço contábil. 
    O desenvolvimento da liderança servidora exige muita motivação, feedback e prática intensiva na vida cotidiana. O que vale é a motivação para mudar e crescer."), "LBTR", null, "L"); 
$pdf->SetFont('Courier','I', 15); 
$pdf->BCell(60, 210, utf8_decode("Não se trata de um exercício intelectual, como aprender álgebra ou a análise de um balanço contábil. 
O desenvolvimento da liderança servidora exige muita motivação, feedback e prática intensiva na vida cotidiana. O que vale é a motivação para mudar e crescer."), "LBTR", null, "C"); 
$pdf->SetFont('ZapfDingbats', null, 10); 
$pdf->TCell(60, 210, utf8_decode("Não se trata de um exercício intelectual, como aprender álgebra ou a análise de um balanço contábil. 
O desenvolvimento da liderança servidora exige muita motivação, feedback e prática intensiva na vida cotidiana. O que vale é a motivação para mudar e crescer."), "LBTR", null, "R"); 
$pdf->Output();