pdf对齐顶部和底部

时间:2012-10-25 12:55:08

标签: php pdf alignment cell fpdf

我需要在pdf文件创建中进行文本对齐(顶部和底部)。

我正在使用这一行:

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

alignment area: L,R,C

2 个答案:

答案 0 :(得分:2)

我发现了一个似乎可以满足您需求的插件。您的位置是:http://fpdf.de/downloads/addons/52/

该插件提供了一个功能

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

允许在由函数参数定义的矩形内对齐文本(水平和垂直)。

答案 1 :(得分:0)

使用TCell将文本和BCell排列在下面的文本行中。

这对我很有用,我希望它对你也有帮助。

window.getComputedStyle(element);

使用方法:

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;
    }

}