How to Add/Set Images on PHPoffice/Phpword Template

How to add/set images on PHPOffice/PHPWord Template?

Following code is the updated version of the one from TotPeRo (thanks again for your code!), for last phpOffice (0.11) that has evolved a little

/**
* Set a new image
*
* @param string $search
* @param string $replace
*/
public function setImageValue($search, $replace)
{
// Sanity check
if (!file_exists($replace))
{
return;
}

// Delete current image
$this->zipClass->deleteName('word/media/' . $search);

// Add a new one
$this->zipClass->addFile($replace, 'word/media/' . $search);
}

Can be called with:

$document->setImageValue('image1.jpg', 'my_image.jpg');

How to add image in the headersection using PHPWORD?

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



Related Topics



Leave a reply



Submit