Phpword table width

PHPWord Table Width

In PHPWord, you can set the width of a table using the setWidth method of the table object. The setWidth method accepts a value in either percentage or absolute units.

Examples:

Example 1: Setting width in percentage


$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$table = $section->addTable();
$table->setWidth('80%'); // Set width to 80% of the page width

Example 2: Setting width in absolute units


$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$table = $section->addTable();
$table->setWidth(500); // Set width to 500 units (e.g., pixels)

Note that if you want to set the table width based on the page width, you need to calculate the percentage manually. For example, if the page width is 800 pixels and you want the table to be 50% wide, then you would set the width to 400 pixels.

Leave a comment