Replies: 1   Views: 1149
Custom table styles: left and right cell margins are ignored
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 Lochspring  · 16-08-2021 - 18:47

Hi there.  We're running PHPDocX 10, and running into a bit of a snag when trying to apply a table style that's embedded in the WORD document to out incoming content.  Our style is set with a cell margin of 0.5 in all the way around, top, bottom, left, and right.  If we manually apply the style in Word, it works flawlessly.  If we attach and apply it using WordStyles as part of embedHTML (or its wrapper function, replaceVariableByHTML), we lose the left and right margins on cells.

From searching the forums, what I've seen is that this is not uncommon.  Generally, what I've read is that there's a need to force those zeroed out values to be something else in HTML2WordML.php if you want to force the application of a value OTHER than 0 when you DON'T have CSS padding values being fed to the translator.  

The challenge, then, is how to extract the custom styles for Tables out of the base Word Template we start with.  Indexer does not seem to retrun the values in a useful way,or at least not in a way we can parse and see the results of our custom styles. 

So, I ask:  what is the most efficient way to get the cell margin values (left and right) from a given custom table style (e.g. CoolTableStyle).  

Posted by admin  · 16-08-2021 - 19:22


When transforming HTML to DOCX the following contents can add spacing in a cell: the cell itself and the paragraph added to the cell. We recommend you to check if that extra spacing is being added in the cell or in the paragraph added as content. The following styles apply spacings: margin and padding (please check you are not adding extra spacings with these styles).
Please also note that if you are applying a custom table style and want to ignore CSS styles as much as possible, you need to use strictWordStyles (as true) and addDefaultStyles (as false).
Using the previous options and the correct margin/padding values you can get the exact output you need.

About your question, you can use getWordStyles to get the styles applied to a custom style. From the sample included in the package (DocxPath/getWordStyles/sample_4.php):

$referenceNode = array(
    'type' => 'style',
    'contains' => 'ListParagraph',

$contents = $docx->getWordStyles($referenceNode);

that returns the styles applied to ListParagraph. You can replace it by any other style name. To get the cell margins in a custom table style you need to query by the w:tblCellMar style (w:top, w:left, w:bottom and w:right tags). Although you can get the style values using this code and apply them to the HTML/CSS, we think the easiest approach is setting the correct styles and options when transforming HTML, that allows to ignore unwanted styles and use the values from the custom styles.

If you open a support ticket ( attaching your DOCX template, the HTML you are transforming, and the code you are using to transform it (in all cases the most simple samples that illustrate your issue), the dev team will check them and generate a custom script using your template and HTML.