skip to Main Content

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 PhpOfficePhpWordTemplateProcessor("test.docx");

$table = new PhpOfficePhpWordElementTable();

$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

Answers


  1. Specify the absolute path to the file and everything will work

    Login or Signup to reply.
  2. I added a new key and used it later to add the image. Worked perfectly.

    $template = new PhpOfficePhpWordTemplateProcessor("test.docx");
    
    $table = new PhpOfficePhpWordElementTable();
    
    $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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search