PhpWord add image to table cell

1.6k Views Asked by At

I want to replace the variable (test_var) inside the document, i can create a table and add texts to it with addText, but the addImage method is not adding the image.

Here is my code:

$template = new \PhpOffice\PhpWord\TemplateProcessor("test.docx");

$table = new \PhpOffice\PhpWord\Element\Table();

$table->addRow();
$table->addCell()->addText("test");
$table->addCell()->addImage("test.png");

$template->setComplexBlock('table_var', $table);

$template->saveAs("test_.docx");
  • PHP version: 7.4
  • PhpWord version: 0.17.0
2

There are 2 best solutions below

2
On

Specify the absolute path to the file and everything will work

0
On

I added a new key and used it later to add the image. Worked perfectly.

$template = new \PhpOffice\PhpWord\TemplateProcessor("test.docx");

$table = new \PhpOffice\PhpWord\Element\Table();

$table->addRow();
$table->addCell()->addText("test");
$table->addCell()->addText("${myImage}");

$template->setComplexBlock('table_var', $table);
$template->setImageValue('myImage', 'test.png');
$template->saveAs("test_.docx");

Good luck!