I get html body from tinymce that contains images and text (paragraph). But I get blank images when I'm trying to insert it to template.
There is my code:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('./template.docx');
$section = $phpWord->addSection();
$content = '<p>Test</p> <img src="https://www.sciencemag.org/sites/default/files/styles/inline__450w__no_aspect/public/sparrow_16x9_0.jpg" />';
Html::addHtml($section, $content);
$xml = getXMLContent($section);
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(false);
$templateProcessor->setValue('test', $xml);
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
$templateProcessor->saveAs('./result.docx');
function getXMLContent($section) {
$xmlWriter = new XMLWriter(XMLWRITER::STORAGE_MEMORY, './', Settings::hasCompatibility());
$containerWriter = new Container($xmlWriter, $section);
$containerWriter->write();
return $xmlWriter->getData();
}
Am I doing something wrong? Or there is an another way to do it?
First of all, which version of PHPWord are you using? If you are using the latest version(as of Oct 2020), you can use this function:
https://phpword.readthedocs.io/en/latest/templates-processing.html#setimagevalue
Adding image to a Word template is as easy as:
setImageValue(<tag>, <path to file>);
I am currently trying to fix the height and width of the image, but I found it to be buggy, with the image width fixed at 3.04cm.
The API to insert image and setting it's height and width:
setImageValue(<tag>, array('path'=><path to file>,'height'=><height>,'width'=><width>);