PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.21k stars 2.68k forks source link

PhpWord addRow doesn't work in Microsoft Word with SetComplexValue #2260

Open TommyB1992 opened 2 years ago

TommyB1992 commented 2 years ago

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

Thank you

Jack-ST94 commented 2 years ago

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(...);