1

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 Answer 1

0

Replace

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

with

$table_style->setUnit(\PhpOffice\PhpWord\SimpleType\TblWidth::PERCENT);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.