Replies: 2   Views: 2444
Html2wordml column size: i've resolved the issuecase 'tr': //before closing a ro
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by matteo.moretti  · 15-05-2017 - 14:40

Hello everybody: I constantly read about issues with column size and so on. In particular we had that issue on HTML2WorldML (docx) that causes two problems:

1) Every column has the same size

2) Colspan did not apply in a proper way

My solution is the following

case 'tr':
  //Before closing a row we should make sure that there are no lacking cells due to a previous rowspan
  $row = count(self::$tableGrid[self::$openTable]) - 1;
  $column = count(self::$tableGrid[self::$openTable][$row]);
  $sRet .= $this->closeTr($row, $column);
  if (strpos(self::$WordML, '#<w:gridCol/>#') !== false) {
    $substitutions = '';
    $cellWithoutExplicitWidth = 0;
    preg_match('/<w:tblW w:w="(\d+)"/', self::$WordML, $result);
    $restOfTableWidth = end($result);
    foreach (self::$tableGrid[self::$openTable][$row] as $column) {
      // get the cell width
      list($cellWidth, $cellWidthType) = $this->_wordMLUnits($column[2]['width']);
      if (!$cellWidth) {
        $cellWidth = '__tbd__';
      } else {
        $restOfTableWidth -= $cellWidth;

      $substitutions .= '<w:gridCol w:w="'.$cellWidth.'"/>';

    if ($cellWithoutExplicitWidth) {
      $cellsWidth = $restOfTableWidth / $cellWithoutExplicitWidth;
      $substitutions = str_replace('__tbd__', $cellsWidth, $substitutions);

    self::$WordML = str_replace('#<w:gridCol/>#', $substitutions, self::$WordML);

  //We now may close the tr tag
  $sRet .= '</w:tr>';

that substitutes what's on HTML2WorlML from line 1348

Previously in the code were not an explicit iteration on td(s) and so every single column has same width (the first one). With my fix, if a column has an explicit dimension, this dimension will be applyed, otherwise a placeholder is defined(__tbd__ ; to be defined) and once I know how many of these columns without a width setted are, I simply divide the dimension of the table (subtracted by the sum of all columns with a width) by the number of these columns.

I've tested in my production enviroment and the issues are gone.

I really appreciate If anyone can take a look to my code and give me a feedback.

Best regards,

Samuele "DonCallisto" Lilli

Posted by admin  · 15-05-2017 - 17:20


Thanks for your patch. We have moved it to the dev team, and it has problems when adding nested tables, but it may be useful for some people with similar issues.

As explained on the documentation pages, we recommend setting exact sizes when transforming HTML with tables, or use the addTable method.

We keep the topic open if any user want to post about it.


Posted by matteo.moretti  · 05-06-2017 - 12:27

I've noticed that there is a little inaccurancy: I've changed a little piece of that code in

$restOfTableWidth = 0;

preg_match_all('/<w:tblW w:w="(\d+)"/', self::$WordML, $result);
if ($result) {
    $restOfTableWidth = end($result[1]);

Don't know what to do (and even if it's a concrete case) when no match at all from preg_match_all but I suppose that's not a possibile scenario as, AFAIK, it seems that at least a 0 is placed.

Best regards,