pdflib table (php) alignin text to right makes it jump up

pdflib table (php) alignin text to the right makes it jump up

我在创建 PDF 时遇到了另一个问题。

我写了一个通用函数,它将创建一个 table。

// to set up a table one needs to supply: 
// the font detailss for 
// the header and the table body (font, fontsize, textPosition)
//
// the multidimensional array to hold info about the header
// headerText, columnWidth
// for advanced users
// extra seetings can be added to each column as a key=>value
// pair in the 'additional' array
//
// table body requires a multidimensional array that will hold
// an array of field values for every row
//
// coordinates for the lower left and upper right corners
// lowerLeftX, lowerLeftY and upperRightX, upperRightY
protected function displayTable(
    $headerFont=array('fontName'=>'', 'fontEncoding'=>'', 'fontSize'=>''), 
    $headers=array( array('headerText'=>'', 'columnWidth'=>'', 'textPosition'=>'',
                        'additional'=>array()), 
                    array('headerText'=>'', 'columnWidth'=>'', 'textPosition'=>'',
                        'additional'=>array())),
    $fieldsFont=array('fontName'=>'', 'fontEncoding'=>'', 'fontSize'=>''), 
    $fields=array(array(    
                    array('value'=>'fieldValue', 'textPosition'=>''),
                    array('value'=>'fieldValue', 'textPosition'=>'')
                    )),
    $lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY,
    $tableOptions = "header=1 rowheightdefault=auto"){

    $table = 0;
    $headFont = 0;

    //generate headers
    $row = 1;
    $headFont = pdf_load_font($this->pdf, $headerFont['fontName'], $headerFont['fontEncoding'], "");

    if ($headFont == 0) {

        die("Error: " . pdf_get_errmsg($this->pdf));

    }
    $col = 0;

    foreach ($headers as $header){

        $col++;

        $optionList = "fittextline={position=".$header['textPosition']." 
                        font=" .$headFont. " 
                        fontsize=".$headerFont['fontSize']."} 
                        colwidth=".$header['columnWidth'];

        if ((isset($header['additional'])) && (!empty($header['additional']))){

            foreach ($header['additional'] as $key=>$value){

                $optionList.=" ".$key."=".$value;

            }

        }

        $table = pdf_add_table_cell($this->pdf,$table, $col, $row, $header['headerText'], $optionList);

        if ($table == 0) {

            die("Error: " . pdf_get_errmsg($this->pdf));

        }

    }

    //the rest of the fields
    $bodyFont = pdf_load_font($this->pdf, $fieldsFont['fontName'], $fieldsFont['fontEncoding'], "");
    if ($bodyFont == 0) {

        die("Error: " . pdf_get_errmsg($this->pdf));

    }

    foreach ($fields as $line){

        $row ++;
        $col = 0;

        foreach($line as $field){

            $col++;

            $optionList = "fittextline={position=".$field['textPosition']." 
                        font=" .$bodyFont. " 
                        fontsize=".$fieldsFont['fontSize']."} ";

            $table = pdf_add_table_cell($this->pdf,$table, $col, $row, $field['value'], $optionList); 

        }

        if ($table == 0) {

            die("Error: " . pdf_get_errmsg($this->pdf));

        }
    }

    // Place the table instance 

    $result = pdf_fit_table($this->pdf, $table, $lowerLeftX, $lowerLeftY, 
                $upperRightX, $upperRightY, $tableOptions);

    if ($result ==  "_error") {

        die("Couldn't place table: " . pdf_get_errmsg($pdf));

    }

     pdf_delete_table($this->pdf, $table, "");

}

自从我更新了热门评论以来,我做了一些更改,但功能相当简单(如果你可以说 PDFLib 相关功能 :'-( ) 并且通常会遵循评论

我需要将其中一个字段中的文本右对齐

当我在另一个 class 中调用该函数并随后将其显示在页面上时,我设置了右对齐的字段会稍微向上跳跃并脱离垂直对齐。我对此感到非常困惑。

调用函数的代码如下

    //set up a table
    parent::displayTable(
    $headerFont=array('fontName'=>'ZEISSFrutigerNextW1G-BoldIt', 'fontEncoding'=>'unicode',
                'fontSize'=>'9'), 
    $headers=array( array('headerText'=>'Artikelnummeb', 'columnWidth'=>60
                    , 'textPosition'=>'left', 'additional'=>array('margin'=>4)), 
                    array('headerText'=>'Artikelbezeichnung', 'columnWidth'=>230
                    , 'textPosition'=>'left', 'additional'=>array('margin'=>4)),
                    array('headerText'=>'EAN', 'columnWidth'=>70
                    , 'textPosition'=>'left', 'additional'=>array('margin'=>2)),
                    array('headerText'=>'Preis (brutto)', 'columnWidth'=>65
                    , 'textPosition'=>'left', 'additional'=>array('margin'=>0))),
    $fieldsFont=array('fontName'=>'ZEISSFrutigerNextW1G-Light', 'fontEncoding'=>'unicode', 
                'fontSize'=>'9'), 
    $fields=array(  array(array('value'=>'01234567890123', 'textPosition'=>'left'), 
                        array('value'=>'fieldValue', 'textPosition'=>'left'), 
                        array('value'=>'8888888888888', 'textPosition'=>'left'), 
                        array('value'=>'99999.99 Eur', 'textPosition'=>'right')),


                    array(array('value'=>'01234567890123', 'textPosition'=>'left'), 
                        array('value'=>'fieldValue', 'textPosition'=>'left'), 
                        array('value'=>'8888888888888', 'textPosition'=>'left'), 
                        array('value'=>'99999.99 Eur', 'textPosition'=>'right')),

                    array(array('value'=>'01234567890123', 'textPosition'=>'left'), 
                        array('value'=>'fieldValue', 'textPosition'=>'left'), 
                        array('value'=>'8888888888888', 'textPosition'=>'left'), 
                        array('value'=>'99999.99 Eur', 'textPosition'=>'right')),

                    array(array('value'=>'01234567890123', 'textPosition'=>'left'), 
                        array('value'=>'fieldValue', 'textPosition'=>'left'), 
                        array('value'=>'8888888888888', 'textPosition'=>'left'), 
                        array('value'=>'99999.99 Eur', 'textPosition'=>'right'))),
    111, 50, 551, 400,
    $tableOptions = "header=1 rowheightdefault=auto ");

结果如下

欢迎任何关于解决此问题的想法

P.S。我确定我只是遗漏了一些可笑的东西,但这些是最糟糕的发现:(

position 选项有一个或两个值。来自 PDFlib API 参考,第 6.1 章,table 6.1:

The keywords left, center, right (in x direction) or bottom, center, top (in y direction) can be used as equivalents for the values 0, 50, and 100. If only one keyword has been specified, the corresponding keyword for the other direction will be added.

在您的代码中,您设置:

       $optionList = "fittextline={position=".$field['textPosition']." 
                    font=" .$bodyFont. " 
                    fontsize=".$fieldsFont['fontSize']."} ";

这意味着,您只应用一个值。当您应用 right(表示 100)时,您将获得与 position={right 100} 相同的值,这表示右上角。

对于您的情况,我建议将代码扩展为:

       $optionList = "fittextline={position={".$field['textPosition']." bottom}  
                    font=" .$bodyFont. " 
                    fontsize=".$fieldsFont['fontSize']."} ";

所以你得到 right bottom 类似于默认的 left bottom