PhpWord addTable/addRow doesn't work in Microsoft Word

Viewed 72

I have this code:

use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\TemplateProcessor as Tpl;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Table;

$file     = '/var/www/html/writable/docs/incarico.docx';
$download = '/var/www/html/writable/docs/new.docx';
$tpl     = new Tpl($file);
$phpWord = new PhpWord();
$section = $phpWord->addSection();

$table = $section->addTable('table');
$table->addRow(); # <--- generate the error
$table->addCell(1750)->addText(htmlspecialchars("test 1"));
$table->addCell(1750)->addText(htmlspecialchars("test 2"));
$tpl->setComplexValue('riferimenti_pratica', $table);

$tpl->saveAs($download);

header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type($download));
header('Content-Disposition: attachment; filename="' . basename($download) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($download));
readfile($download);
exit;

And this code works well in openoffice and some online viewers, but I cant' open it in microsoft word. The problem is with addRow() method. It works if I comment these lines (of course nothing happen but at least I can open it with microsoft word):

$table->addRow();
$table->addCell(1750)->addText(htmlspecialchars("test 1"));
$table->addCell(1750)->addText(htmlspecialchars("test 2"));

The version of phpword is 0.18.3 and the version of php is 8.1.7

0 Answers
Related