Forum


Replies: 5   Views: 375
Table loses cell size when viewing in google docs
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 Elizabeth Cruz  · 20-06-2024 - 15:42

Hi, community!!

I am doing some formats with phpdocx v6. I use tables to place content, when I open the format in Microsoft Word it is displayed correctly, but when I open it in Google Docs the table loses the size of the cells.
In the same way, if I edit the file in Microsoft Word and then view it in Google Docs, it correctly maintains the size of the cells and all the formatting.

Is there a way to display the Word correctly once phpdocx creates it?

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.

Posted by Elizabeth Cruz  · 20-06-2024 - 16:46

Thanks for your response, I will try to update the version and set the cell sizes to fixed.

Posted by Elizabeth Cruz  · 21-06-2024 - 16:41

One last question, if I update the library to the latest version, should I no longer set the fixed column values or should it also be done for this latest version?

Posted by admin  · 21-06-2024 - 17:12

Hello,

All phpdocx versions require using fixed column values to not use auto widths. If you don't set fixed column widths when creating a table, auto widths are used.

Regards.

Posted by Elizabeth Cruz  · 21-06-2024 - 17:15

Ok. Thank you so much!