PHPWord
PHPWord copied to clipboard
PhpWord addRow doesn't work in Microsoft Word with SetComplexValue
Describe the Bug
When I try to insert a generated table in a processed file opened throught TemplateProcessor I can't open it in microsoft word but in open office yes. It's the same with online services: the online service provided by microsoft for managing .docx files doesn't work, others services (that probably uses open office in the background) yes.
Steps to Reproduce
<?php
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);
exit;
Current Behavior
I can't open the file in microsoft word (I have a generic error alert).
Context
- PHP Version: 8.1.7
- PHPWord Version: 0.18.3
Thank you
Try with $tpl->setComplexBlock(), I also tried to add tables to a template using ->setComplexValue(), but it just didn't work.
If you still get the error, you can try to create a Table object directly.
$table = new Table(...);
$table->addRow(); $table->addCell(... $table->addCell(...
$tpm->setComplexBlock('placeholder', $table'); $tpl->saveAs(...);
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue for you, please try to help by debugging it further and sharing your results. Thank you for your contributions.