Replies: 3   Views: 204
Table cell spacing adding empty cells
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 websurfermedia  · 13-10-2022 - 05:12

Hi Guys,

Having an issue with a table when using embedHtml

When the cell is blank, the formatting is off. See screenshot at


Posted by admin  · 13-10-2022 - 06:46


MS Word doesn't allow adding empty cells (a corrupted DOCX message appears if a cell doesn't have at least one content). If a cell doesn't contain at least one content, an empty paragraph without styles is added by phpdocx when transforming the HTML to avoid generating a corrupted DOCX. For this reason, we recommend adding at least one space to generate all sizes in the same way:

<td width="30%"><b>Last Name</b></td>
<td width="70%">&nbsp;</td>

otherwise, an empty paragraph is generated. As this paragraph doesn't apply styles, the default ones are used.
Adding some default styles can be avoided by disabling the addDefaultStyles option:

$docx->embedHTML($html, array('addDefaultStyles' => false));

but this approach requires using paragraphs in cells to set custom pararagraph spacings with CSS styles.

These are the available approaches using the current stable version. We have also sent your message to the dev team to be checked and improve the transformation in this case.


Posted by admin  · 13-10-2022 - 13:58


The dev team has generated a patch in the testing branch. Although it's currently being tested before including it in the stable branch, it can suit your needs without adding empty blank spaces as explained in our previous reply.

Please edit the HTMLWordML.php file and go to the repairWordML method. In this method, replace the following line:

$data = str_replace('</w:tcPr></w:tc>', '</w:tcPr><w:p /></w:tc>', $data);


$data = str_replace('</w:tcPr></w:tc>', '</w:tcPr><w:p><w:pPr><w:spacing w:after="0" w:before="0" w:line="240" w:lineRule="auto"/></w:pPr></w:p></w:tc>', $data);


Posted by websurfermedia  · 13-10-2022 - 22:24

Thanks guys, this patch a much better solution that going trhough a very large html template and adding