Forum


Replies: 2   Views: 2463
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__';
        $cellWithoutExplicitWidth++;
      } 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>';
  break;

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