PHP RTF 精简版。经过一定次数的循环后 table 单元格的高度越来越高

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

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

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

下载 class 并尝试以下操作:

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