Forum


Replies: 3   Views: 29
Setting td widths using embedhtml

Posted by matteo.moretti  · 24-04-2025 - 10:07

After upgrading from v14.5 to v16 I noticed a braking change in the behavior of embedHTML function.
This is my code snippet:

$document = new CreateDocx();
$documento->embedHTML($html, [
    'parseDivsAsPs' => true,
    'downloadImages' => true, 
]);

The HTML I'm using for testing is the following

<div>
<table style="border-collapse: collapse; width: 100%; height: 90px;" border="1">
<tbody>
<tr style="height: 18px;">
<td style="width: 14.8275%; height: 18px;">aaaaa</td>
<td style="width: 18.5059%; height: 18px;">&nbsp;</td>
<td style="width: 20.4814%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
<td style="width: 20.8901%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr style="height: 18px;">
<td style="width: 14.8275%; height: 18px;">&nbsp;</td>
<td style="width: 18.5059%; height: 18px;">&nbsp;</td>
<td style="width: 20.4814%; height: 18px;" rowspan="3">ccccccc</td>
<td>&nbsp;</td>
<td style="width: 20.8901%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr style="height: 18px;">
<td style="width: 14.8275%; height: 18px;">&nbsp;</td>
<td style="width: 18.5059%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
<td style="width: 20.8901%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr style="height: 18px;">
<td style="width: 14.8275%; height: 18px;">&nbsp;</td>
<td style="width: 18.5059%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
<td style="width: 20.8901%; height: 18px;">dcdscds</td>
<td>&nbsp;</td>
</tr>
<tr style="height: 18px;">
<td style="width: 14.8275%; height: 18px;">&nbsp;</td>
<td style="width: 18.5059%; height: 18px;">&nbsp;</td>
<td style="width: 20.4814%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
<td style="width: 20.8901%; height: 18px;">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
</div>

Before the upgrade the output was a 6 columns layout now I only have 4 columns (<td> elements without an explicit width are being removed!).
How can I get back to the previous behavior?

Posted by admin  · 24-04-2025 - 12:06

Hello,

We have run your script with phpdocx 14.5 and phpdocx 16, and both versions generate the same output.
Maybe you did some custom changes in your phpdocx 14.5 installation? If you send the HTML2WordML.php file from your phpdocx 14.5 to contact[at]phpdocx.com we'll compare it with the default version to check if any changes have been made.

All columns appear correctly if you open the DOCX outputs with MS Word. We have tested them with MS Word 2010, MS Word 2013, MS Word 2016, MS Word 2019, MS Word 2024, and MS Word 365.

Google Docs and MS Word Online also display six columns in the table.

It seems you are opening the DOCX with LibreOffice, which requires setting all (or none) cell widths to get a correct output. Please set all cell widths (or do not set cell widths to use auto sizes), but avoid adding widths to only some of the cells.

To get the maximum compatibility with all DOCX readers, we recommend setting table and cell widths when adding tables: https://www.phpdocx.com/documentation/cookbook/convert-html-to-word (Defining widths in tables section). All MS Word versions handle auto sizes correctly, but some DOCX readers have limitations in reading auto sizes.

Regards.

Posted by matteo.moretti  · 24-04-2025 - 12:37

Thank you, I found the issue.

I had exactly added some code to the HTML2WordML class.

I applied again the changes to the new installation and everything is working fine now!

Posted by admin  · 24-04-2025 - 12:39

Hello,

Please send the HTML2WordML class file you are using to contact[at]phpdocx.com. We'll send it to the dev team. Maybe the custom changes you have added can be included in the stable release if all tests pass correctly.

Regards.