Forum


Replies: 5   Views: 312
Table loses cell size when viewing in google docs

Posted by admin  · 20-06-2024 - 16:17

Hello,

If you are using phpdocx 6 (a very old version not compatible with the latest versions of MS Word and PHP), first we recommend you upgrade to the latest version of phpdocx (phpdocx 15), which includes many new features and options (https://www.phpdocx.com/news/). You can upgrade the license on MY PHPDOCX page after login.

Regarding your question, MS Word supports auto sizes applied in tables, but other DOCX readers (such as Google Docs that has many limitations and doesn't support some contents and styles from the OOXML standard) don't support using auto sizes and the table output won't appear as expected. In this case, the best approach is applying fixed column sizes. You can do this using addTable (columnWidths table option):

$options = array(
    'columnWidths' => array(6000, 1500, 1500),
    'border' => 'single',
);
$values = array(
    array('Item A', '10', '11'),
    array('Item B', '20', '21'),
    array('Item C', '30', '31'),
    array('Item D', '40', '41'),
);
$docx->addTable($values, $options);

and embedHTML (width attribute) using attributes and inline styles:

$html = '<table border="1" style="border-collapse: collapse" width="100%">
            <tbody>
                <tr>
                    <td width="400">HTML A</td>
                    <td width="100">1A</td>
                    <td width="100">2A</td>
                </tr>
                <tr>
                    <td width="400">HTML B</td>
                    <td width="100">1B</td>
                    <td width="100">2B</td>
                </tr>
                <tr>
                    <td width="400">HTML C</td>
                    <td width="100">1C</td>
                    <td width="100">2C</td>
                </tr>
                <tr>
                    <td width="400">HTML D</td>
                    <td width="100">1D</td>
                    <td width="100">2D</td>
                </tr>
            </tbody>
        </table>';
$docx->embedHTML($html);

or using a style tag (the nth-of-type CSS selector used in this approach may not be supported by the old phpdocx 6 version):

$html = '
    <style>
        table {
            width: 100%;
        }
        td:nth-of-type(1) {
            width: 400px;
        }
        td {
            width: 100px;
        }
    </style>
    <table border="1" style="border-collapse: collapse">
            <tbody>
                <tr>
                    <td>HTML A</td>
                    <td>1A</td>
                    <td>2A</td>
                </tr>
                <tr>
                    <td>HTML B</td>
                    <td>1B</td>
                    <td>2B</td>
                </tr>
                <tr>
                    <td>HTML C</td>
                    <td>1C</td>
                    <td>2C</td>
                </tr>
                <tr>
                    <td>HTML D</td>
                    <td>1D</td>
                    <td>2D</td>
                </tr>
            </tbody>
        </table>';
$docx->embedHTML($html);

Applying fixed widths is compatible with all DOCX readers.

Regards.