We use phpdocx to create a table from a HTML source in an existing Word document. For this, we use the CreateDocxFromTemplate functionality in PHPDocx version 10.
In the Word template, a default table style is defined. In this style, the margins of each cell are set to 0.19 cm. The HTML <table> element has a css class, which we map to this Word style by setting strictWordStyles to true in a call to replaceVariableByHTML.
The HTML table is then converted to Word, and the intended table style is correctly applied. However, in this conversion, the margins of each table cell are set to 0 mm instead of the expected 0.19mm. This looks awkward, all text is cramped together.
I dug a bit deeper in the phpdocx source code, and found the following cause:
- For conversion of HTML, the DOMPDF library is used by phpdocx.
- This library sets the default table cell margin to 0, in the variable $aDefaultProperties.
- If nothing is done after initializing a Word table cell, all margins will be kept at 0.
- In HTML2WordML.php, custom css styles are checked. Margins are only applied if custom css properties padding-left or padding-right are present. If they are not present, the default value 0 will be kept.
- I think the correct default value would be null instead of 0. Then, maybe you can just omit a margin, and the Word table style will manage margins.
My HTML table does not have any styles or classes, except for the <table> itself to determine the Word style to apply to the whole table. So, the default margin of 0 is applied to all table cells in the Word document.
I cannot add screenshots to this post, maybe I can send screenshots in another manner?
Can you please have a look at this? Please let me know if you need more information!