PHP RTF Lite.循环一定数量后,表格单元格的高度会越来越高


PHP RTF Lite. After a certain number of loops table cells increase height more and more

我在使用PHPRtfLite类在RTF文件的表单元格中写入文本时遇到了一种奇怪的行为。数据是从XML中提取的,但奇怪的行为可以用一个简单的数组来重现。

我注意到经过一定数量的循环后,单元格的高度(底部填充)会增加。如果我向数组添加一个值(因此循环增加一),则填充将增加,比如说X,如果我添加两个值,则填充会增加X*2,等等。这只发生在上的某个循环中。从第一个循环开始就没有了。

下载该类并尝试以下操作:

  1. 首先,按原样尝试代码(单元格将有底部填充)
  2. 其次,从数组中删除一个值,然后重试(底部填充为零)
  3. 第三,将TWO OR MODE值添加到数组中(单元格填充将按比例增加)

另一件奇怪的事情是,Word本身无法设置单元格的底部边框。用Word加载RTF并尝试修复它。不能将底部边框移向顶部。它被卡住了,没有添加空行,也没有设置单元格高度。

    <?php
    error_reporting(E_ALL & ~E_NOTICE);
    require_once('PHPRtf/lib/PHPRtfLite.php');

    $cars = array("Acura", "Alfa Romeo", "Aston Martin", "Audi", "Bentley", "BMW", "Bugatti", "Buick", "Cadillac", "Chevrolet", "Chrysler", "Citroen", "Dodge", "Ferrari", "FIAT", "Ford", "Geely", "GM", "GMC", "Honda", "Hyunday", "Infiniti", "Jaguar", "Jeep", "Kia", "Koenigsegg", "Lamborghini", "Land Rover", "Lexus", "Maserati", "Mazda", "McLaren");

    PHPRtfLite::registerAutoloader();
    $rtf = new PHPRtfLite();
    $border = new PHPRtfLite_Border(
        $rtf,
        new PHPRtfLite_Border_Format(1, '#000000'), // left border
        new PHPRtfLite_Border_Format(1, '#000000'), // top border
        new PHPRtfLite_Border_Format(1, '#000000'), // right border
        new PHPRtfLite_Border_Format(1, '#000000')  // bottom border
    );

    $font = new PHPRtfLite_Font(12, 'DecimaWE Rg', '#000000', '#FFFFFF');
    $justify = new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_JUSTIFY);
    $sect = $rtf->addSection();
    $table = $sect->addTable();

    $row = 1;
    foreach($cars as $value) {
        // Add a row of height 0 at every loop
        $table->addRows(1, 0);
        // Set two columns of the same width
        $table->addColumnsList(array(8.5, 8.5));
        // Set left cell 
        $cell = $table->getCell($row, 1);
        $cell->setCellPaddings(0.4, 0, 0.4, 0);
        $cell->setFont($font);
        $cell->setTextAlignment(PHPRtfLite_Table_Cell::TEXT_ALIGN_JUSTIFY);
        $cell->setBorder($border);
        // Set right cell 
        $cell = $table->getCell($row, 2);
        $cell->setCellPaddings(0.4, 0, 0.4, 0);
        $cell->setFont($font);
        $cell->setTextAlignment(PHPRtfLite_Table_Cell::TEXT_ALIGN_JUSTIFY);
        $cell->setBorder($border);
        // Write in the left cell 
        $table->writeToCell($row, 1, $value);
        // Write in the right cell 
        $table->writeToCell($row, 2, $value);
        $row++;
    }   
    $rtf->save('cells.rtf');
?>

我自己解决。

线路:

$table->addColumnsList(array(8.5, 8.5));

必须在循环之外,并且应放置在addTable:之后

$table = $sect->addTable();    
$table->addColumnsList(array(8.5, 8.5));