Search code examples
phpword

How to add image in the headersection using PHPWORD?


I am using phpword and yii , here i want to generate docx file which includes an image at the top right corner.Here is the code that i have been used

 $headert = $section->createHeader();

 $table = $headert->addTable();
 $table->addRow();
 $table->addCell(4500)->addText('This is the header.');
 $table->addCell(4500)->addImage('/home/dev238/projects/aegis/images/Logo_.jpg',
                                 array('width'  => 50,
                                       'height' => 50,
                                       'align'  => 'right'));

The result is image displaying at the top left part inside document and not at the header section . I am using LibreOffice for viewing


Solution

  • Can you post more of the code please?

    Meanwhile, I think the createHeader(); function has been deprecated. The addHeader(); function worked fine for me. You could give it a try too. Also, consider bringing down the width of the cells being created. Of course, these are wild guesses but who knows one of them might get things working for you. ;-)

    Good Luck!

    Edit 2020/02/12: createHeader method is nowadays deprecated, use addHeader instead.

    $header = $section->addHeader();
    

    Reference: https://phpword.readthedocs.io/en/latest/containers.html#headers