0

I'm experiencing a weird behaviour writing text inside the table cells of a RTF file using the PHPRtfLite class. Data is extracted from a XML but the weird behaviour may be reproduced with a simple array.

I noticed that after a certain number of loops the height (bottom padding) of the cells increases. If I add a ONE value to the array (so the loop increments by one) the padding will increase let say of X, if I add TWO values the padding will increase X*2, etc. This happens only from a certain loop on. Not since the first loop.

Download the class and try the following:

  1. Firstly, try the code as is (cells will have a bottom padding)
  2. Secondly, remove ONE value from the array and try again (the bottom padding is zero)
  3. Third, add TWO OR MODE values to the array (the cell padding will increase proportionally).

An other weird thing is that the bottom border of the cell cannot be set by Word itself. Load the RTF with Word and try to fix it. The bottom border cannot be moved towards the top. It is stucked, no blank lines are added, nor cell height is set.

    <?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');
?>
Nicero
  • 4,181
  • 6
  • 30
  • 52

1 Answers1

0

I work it out by myself.

The line:

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

must be outside the loop and should be placed after addTable:

$table = $sect->addTable();    
$table->addColumnsList(array(8.5, 8.5));
Nicero
  • 4,181
  • 6
  • 30
  • 52