Autofit PhpWord table to window

132 views Asked by At

I want to autofit phpword table to window. So, I has add array('unit' => 'pct', 'width' => 5000) when create table. But , the table column show in reversed order in output docx file. I don't want to reverse the column order.

<?php
 include_once '../vendor/autoload.php';
 $phpWord = new \PhpOffice\PhpWord\PhpWord();
 use PhpOffice\PhpWord\Element\Table;
 use PhpOffice\PhpWord\TemplateProcessor;
 use PhpOffice\PhpWord\ComplexType\TblWidth;
 use PhpOffice\PhpWord\Settings;
 use PhpOffice\PhpWord\SimpleType\Border;
 //$styleCell = array('borderColor' =>'black', 'borderSize' => 2);
 //$fontStyle=array('align'=>'center');

 $section = $phpWord->addSection();
 $table = $section->addTable(array('unit' => 'pct', 'width' => 5000));
//$table = new Table();

$table->addRow();
$table->addCell(700)->addText('cell 1');
$table->addCell(500)->addText('cell 2');

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('1711output.docx');

?>

output

I have tried using setWidth method

$table_style = new \PhpOffice\PhpWord\Style\Table;
$table_style->setUnit(\PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT);
$table_style->setWidth(100 * 50);
$table = $section->addTable($table_style);

I got error Uncaught Error: Undefined class constant 'WIDTH_PERCENT'

1

There are 1 answers

0
Gregor Klarič On

Replace

$table_style->setUnit(\PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT);

with

$table_style->setUnit(\PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT);