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!

Posted by admin  · 09-09-2020 - 16:48

Hello,

The current version of phpdocx sets default values to cell margins (using a custom table style or not), so you need to overwrite them with padding properties if you want to set custom values. 

The testing branch adds the following change (not available in the stable branch):

if (!$this->strictWordStyles) {
    if (isset($properties['padding_left']) || isset($properties['padding_right'])) {
        $stringTblPr .='<w:tblCellMar>';
            if (isset($properties['padding_left'])) {
                $paddingLeftValue = $this->_wordMLUnits($properties['padding_left']);
                $stringTblPr .= '<w:left w:type="dxa" w:w="'.$paddingLeftValue[0].'"/>';
            }
            if (isset($properties['padding_right'])) {
                $paddingRightValue = $this->_wordMLUnits($properties['padding_right']);
                $stringTblPr .= '<w:right w:type="dxa" w:w="'.$paddingRightValue[0].'"/>';
            }
        $stringTblPr .='</w:tblCellMar>';
    }
}

So no padding value is added to w:tblCellMar if strictWordStyles is set to true. This change will be added to the next stable version of phpdocx (there's no release date); you can apply the same change to phpdocx 10 in generateTblPr.

Other approach, that doesn't require changing the library, is using DOCXPath (https://www.phpdocx.com/api-documentation/docx-path/remove-elements-in-docx) to remove that cell margins, but we think the first solution is the easiest one.

Regards.

Posted by jeroen8087  · 10-09-2020 - 08:04

The solution you posted works very well, thank you very much for your support!

We look forward to the next release :)