Forum


Replies: 4   Views: 87
Table in table cell without padding

Posted by admin  · 03-12-2024 - 11:37

Hello,

We recommend you remove the cell margin from the inner table, not from the outer table. Unless you apply a margin/spacing style to specific contents, all cell contents get the same cell margin.

As detailed on the addTable API documentation page (https://www.phpdocx.com/api-documentation/word-content/add-table-Word-document-with-PHP), you can use the cellMargin option to set the cell margins applied to all cells in the table:

$table = [
    [
        $col1,
        $col2,
    ]
];

$tableOptions = [
    'tableLayout' => 'fixed',
    'tableStyle' => 'PlainTablePHPDOCX',
    'cellMargin' => [
        'left' => 0,
        'top' => 0,
        'bottom' => 0,
        'right' => 0,
    ],
];

$docx->addTable($table, $tableOptions);

or to specific cells:

$table = [
    [
        [
            'value' => $col1,
            'cellMargin' => [
                'left' => 0,
                'top' => 0,
                'bottom' => 0,
                'right' => 0,
            ],
        ],
        [
            'value' => $col2,
            'cellMargin' => [
                'left' => 200,
                'top' => 0,
                'bottom' => 0,
                'right' => 200,
            ],
        ]
    ]
];

$tableOptions = [
    'tableLayout' => 'fixed',
    'tableStyle' => 'PlainTablePHPDOCX',
];

$docx->addTable($table, $tableOptions);

Also please note that using a fixed layout, we recommend you set the column widths:

$table = [
    [
        [
            'value' => $col1,
            'cellMargin' => [
                'left' => 0,
                'top' => 0,
                'bottom' => 0,
                'right' => 0,
            ],
        ],
        [
            'value' => $col2,
            'cellMargin' => [
                'left' => 200,
                'top' => 0,
                'bottom' => 0,
                'right' => 200,
            ],
        ]
    ]
];

$tableOptions = [
    'tableLayout' => 'fixed',
    'tableStyle' => 'PlainTablePHPDOCX',
    'columnWidths' => [1400,1400],
];

$docx->addTable($table, $tableOptions);

Otherwise, you can get strange cell outputs when applying custom margins.

Regards.