Search code examples
phpword

Align text in table cell to right in PHPWord 0.12.0


I am using PHPWord 0.12.0. I have a table created, but cannot seem to align text in the cells. Here is my code for the specific line:

$table->addCell(1540, array('bgColor' => 'dddddd'))->addText(htmlspecialchars("Testing", array('align' => 'right')));

I have also tried:

$table->addCell(1540, array('bgColor' => 'dddddd', 'align' => 'right'))->addText(htmlspecialchars("Testing"));

The background color shows up fine, but I cannot get the alignment to work. There is no "align" in the specs for a table cell, so what is the proper way to do this?


Solution

  • It would seem that in order to use a paragraph style that PHPWord also requires a font style. So, with a font style defined I can simply use:

    $table->addCell(1540, array('bgColor' => 'dddddd'))->addText(htmlspecialchars("Testing the alignment"), $myfontstyle, array('align' => 'right'));
    

    This works. The font style $myfontstyle has to be defined in your code. Or you can put the array inside the code as I did for the paragraph style.