Forum


Replies: 2   Views: 1889
Margins are set to 0 when converting a html table to word
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 jeroen8087  · 09-09-2020 - 14:18

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:

  1. For conversion of HTML, the DOMPDF library is used by phpdocx.
  2. This library sets the default table cell margin to 0, in the variable $aDefaultProperties.
  3. If nothing is done after initializing a Word table cell, all margins will be kept at 0.
  4. 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.
  5. 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!