Align text in table cell to right in PHPWord 0.12.0

kojow7 picture kojow7 · Aug 27, 2015 · Viewed 7.8k times · Source

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?

Answer

kojow7 picture kojow7 · Aug 27, 2015

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

And it works. Of course, you must have defined a font style called $myfontstyle earlier in your code (or place the array inside the code as I did for the paragraph style).