Forum


Replies: 5   Views: 2992
Html table default cellmarges
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 Simco Nederland  · 05-01-2018 - 13:54

<table style="border:0px; float:right; width:100%; boder-spacing:0; border-collapse: collapse; text-align:right; margin:0; padding:0;" cellspacing="0" cellpadding="0">
        <tr valign="top" style="margin:0; padding:0;">
                <td style="margin:0; padding:0;">some data</td>
                <td style="margin:0; padding:0;">another data</td>
        </tr>
</table>

have something like this in my html template
when i create docx the table has cellmarginleft 0.19cm and right 0.19 cm top or bottom are 0.
how is it possible to remove those default cellmarges ?

other solution was to use p tags but it sets them on new lines despite display:inline css
 

Posted by admin  · 05-01-2018 - 15:59

Hello,

The current version of phpdocx only allows using margin-top and margin-bottom for cells. You could MS Word custom styles to get the output you need, but the next version of phpdocx will include support for margin-left and margin-right for cells (other contents already support them).

It's current being tested, but if you want to test it, please edit the HTML2WordML.inc file and replace the tcPrSpacing method by these new code:

private function tcPrSpacing($properties)
{
    $top = $left = $bottom = $right = array(0, 'auto');

    if (!empty($properties['margin_top'])) {
        $top = $this->_wordMLUnits($properties['margin_top'], $properties['font_size']);
    }
    if (!empty($properties['padding_top'])) {
        $temp = $this->_wordMLUnits($properties['padding_top'], $properties['font_size']);
        if ($temp[0] > $top[0])
            $top = $temp;
    }

    if (!empty($properties['margin_bottom'])) {
        $bottom = $this->_wordMLUnits($properties['margin_bottom'], $properties['font_size']);
    }
    if (!empty($properties['padding_bottom'])) {
        $temp = $this->_wordMLUnits($properties['padding_bottom'], $properties['font_size']);
        if ($temp[0] > $bottom[0])
            $bottom = $temp;
    }

    if (!empty($properties['margin_left'])) {
        $left = $this->_wordMLUnits($properties['margin_left'], $properties['font_size']);
    }
    if (!empty($properties['padding_left'])) {
        $temp = $this->_wordMLUnits($properties['padding_left'], $properties['font_size']);
        if ($temp[0] > $left[0])
            $left = $temp;
    }

    if (!empty($properties['margin_right'])) {
        $right = $this->_wordMLUnits($properties['margin_right'], $properties['font_size']);
    }
    if (!empty($properties['padding_right'])) {
        $temp = $this->_wordMLUnits($properties['padding_right'], $properties['font_size']);
        if ($temp[0] > $right[0])
            $right = $temp;
    }

    $spacing = '<w:tcMar>';
    $spacing .= '<w:top w:w="' . $top[0] . '" w:type="' . $top[1] . '"/>';
    $spacing .= '<w:left w:w="' . $left[0] . '" w:type="' . $left[1] . '"/>';
    $spacing .= '<w:bottom w:w="' . $bottom[0] . '" w:type="' . $bottom[1] . '"/>';
    $spacing .= '<w:right w:w="' . $right[0] . '" w:type="' . $right[1] . '"/>';
    $spacing .= '</w:tcMar>';
    return $spacing;
}

After applying these changes, both left and right margins will work for cells.

Regards.

Posted by Simco Nederland  · 08-01-2018 - 08:08

thx. after applying this code it did take negative numbers to set it right with the rest of the elements.
table still had 0.19cm margin left and right.

Posted by admin  · 08-01-2018 - 08:40

Hello,

We have checked it again, and the output is correct.

After applying the previous code, if you run this sample script:

<?php

require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();
$html = '<table style="border:0px; float:right; width:100%; boder-spacing:0; border-collapse: collapse; text-align:right; margin:0; padding:0;" cellspacing="0" cellpadding="0">
        <tr valign="top" style="margin:0; padding:0;">
                <td style="margin:0; padding:0;">some data</td>
                <td style="margin:0; padding:0;">another data</td>
        </tr>
</table>';
$docx->embedHTML($html);

$docx->createDocx('output');

and then you open the DOCX with MS Word and go to the table properties and Cell Options, all margins are set to 0.

If we open the main document.xml file, we check directly that all margins of the cell are set to 0 as well:

<w:tc>
    <w:tcPr>
        <w:tcMar>
            <w:top w:type="auto" w:w="0"/>
            <w:left w:type="auto" w:w="0"/>
            <w:bottom w:type="auto" w:w="0"/>
            <w:right w:type="auto" w:w="0"/>
        </w:tcMar>
        <w:vAlign w:val="top"/>
    </w:tcPr>

Please check that the previous has been applied correctly and run the script of this update standalone. We have checked it with MS Word 2007, 2010, 2013 and 2016 and it's working fine.

Anyway, please note that you can use a custom MS Word style for the table if you prefer. There're samples of using this approach on https://www.phpdocx.com/documentation/introduction/html-to-word-PHP (Using native Word formatting with HTML section).

Regards.

Posted by admin  · 08-01-2018 - 09:05

Hello again,

Doing some more checks, we think that maybe you are not trying to change the cell option margins (Cell tab in the Table Properties) but the Default cell margins value (Table tab in the Table Properties). The previous patch changes cell option margin values, not default cell margings.

This Default cell margins are applied using a MS Word style. If you want to change it, please set a custom MS Word style to the table when importing the HTML, or if you want to apply it for all tables, you could overwrite the default style in the OOXMLResources.inc file:

<w:style w:type="table" w:styleId="NormalTablePHPDOCX">
    <w:name w:val="Normal Table PHPDOCX"/>
    <w:uiPriority w:val="99"/>
    <w:semiHidden/>
    <w:unhideWhenUsed/>
    <w:qFormat/>
    <w:pPr>
        <w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
    </w:pPr>
    <w:tblPr>
        <w:tblInd w:w="0" w:type="dxa"/>
        <w:tblCellMar>
            <w:top w:w="0" w:type="dxa"/>
            <w:left w:w="108" w:type="dxa"/>
            <w:bottom w:w="0" w:type="dxa"/>
            <w:right w:w="108" w:type="dxa"/>
        </w:tblCellMar>
    </w:tblPr>
</w:style>

You can change both w:left and w:right tags, setting w:w="0" to change the default behavior.

Regards.

Posted by Simco Nederland  · 08-01-2018 - 09:33

thx, yes was wanted to change default. but it's ok now. since i can add negative value on td element.