Forum


Replies: 1   Views: 2388
Tables not autofitting content
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 admin  · 23-09-2018 - 11:54

Hello,

The issue comes from the default width. If you open the table properties with MS Word, you can see that "Automatically resize to fit contents" is enabled, but as the script doesn't set a table width, then phpdocx sets it as 100% pct.

You can set a custom table width (as 0) to get the output you want:

$docx = new CreateDocx();

$valuesTable = array(
    array(
        11,
        12,
        13,
        14
    ),
    array(
        21,
        22,
        23,
        24
    ),
    array(
        31,
        32,
        33,
        34
    ),

);

$paramsTable = array(
    'border' => 'single',
    'tableAlign' => 'center',
    'borderWidth' => 10,
    'borderColor' => 'B70000',
    'textProperties' => array('bold' => true, 'font' => 'Algerian', 'fontSize' => 18),
    'tableLayout' => 'autofit', // this is the default value
    'tableWidth' => array('type' =>'pct', 'value' => 0),
);

$docx->addTable($valuesTable, $paramsTable);

$docx->createDocx('output');

Adding that line to your script, the autofit will work perfectly:

'tableWidth' => array('type' =>'pct', 'value' => 0)

Also please note that as many DOCX readers can't handle auto values correctly, we recommend adding fixed sizes to get the best compatibility. We are going to add this information to the API page.

Regards.