Forum


Replies: 3   Views: 2602
Libreoffice conversion - table columns width
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 admin  · 10-07-2019 - 14:47

Hello,

You are not doing anything wrong. The issue is that phpdocx 8.0 needs as many (or more) rows as columns when setting widths to get a proper output with LibreOffice; for example:

$html .= '<table border="1" style="border-collapse: collapse" width="600px">
            <tbody>
                <tr width="600px">
                    <td width="100px">3_2</td>
                    <td width="50px">3_3</td>
                    <td width="450px">3_3</td>
                </tr>
                <tr width="600px">
                    <td width="100px">3_2</td>
                    <td width="50px">3_3</td>
                    <td width="450px">3_3</td>
                </tr>
                <tr width="600px">
                    <td width="100px">3_2</td>
                    <td width="50px">3_3</td>
                    <td width="450px">3_3</td>
                </tr>
            </tbody>
        </table>';

phpdocx 8.5 added some improvements to get a proper output using all DOCX viewers with any number of rows and columns.

If you want to update your file, please edit HTML2WordML.php and in the closePreviousTags method, you need to replace two case lines ('table' and 'tr') with these lines:

case 'table':
    if (self::$openPs) {
        if (self::$openLinks) {
            $sRet.= '</w:hyperlink>';
            self::$openLinks = false;
        }
        if (self::$openBookmark > 0) {
            $sRet.= '<w:bookmarkEnd w:id="' . self::$openBookmark . '" />';
            self::$openBookmark = 0;
        }
        $sRet .= '</w:p></w:tbl>';
        self::$openPs = false;
    } else {
        if (self::$openTable > 1) {
            //This is to fix a Word bug that does not allow to close a table and write just after a </w:tc>
            $sRet .= '</w:tbl><w:p />';
        } else {
            $sRet .= '</w:tbl>';
        }
    }

    // clean previous gridCol placeholder to prevent adding extra tags when adding more than one table
    if (count($this->gridColValues) > 0) {
        foreach ($this->gridColValues as $gridColValue) {
            self::$WordML = str_replace('#<w:gridCol/>#', '<w:gridCol w:w="'.$gridColValue.'"/>#<w:gridCol/>#', self::$WordML);
        }
    } else {
        self::$WordML = str_replace('#<w:gridCol/>#', str_repeat('<w:gridCol w:w="1"/>', $column), self::$WordML);
    }
    self::$WordML = str_replace('#<w:gridCol/>#', '', self::$WordML);
    $this->gridColValues = array();
    
    self::$openTable--;
    break;
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) {
        // get the cell width
        if (self::$tableGrid[self::$openTable][$row][$row][2]['width'] !== null) {
            list($cellWidth, $cellWidthType) = $this->_wordMLUnits(self::$tableGrid[self::$openTable][$row][$row][2]['width']);
        }

        if ($cellWidth) {
            $i = 0;
            foreach (self::$tableGrid[self::$openTable][$row] as $rowProperties) {
                list($cellWidth, $cellWidthType) = $this->_wordMLUnits($rowProperties[2]['width']);
                if (!isset($this->gridColValues[$i]) || $this->gridColValues[$i] > $cellWidth) {
                    $this->gridColValues[$i] = $cellWidth;
                }
                $i++;
            }
        }
    }
    //We now may close the tr tag
    $sRet .= '</w:tr>';
    break;

After applying this change your HTML will work perfectly with LibreOffice.

We have sent the full class with these same changes to your e-mail.

Regards.