有没有办法将图像(html)插入PHPWord模板?

有没有办法将图像(html)插入PHPWord模板?,php,phpword,Php,Phpword,我从tinymce获取html正文,其中包含图像和文本(段落)。但当我试图将其插入模板时,会得到空白图像 这是我的代码: $phpWord = new \PhpOffice\PhpWord\PhpWord(); $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('./template.docx'); $section = $phpWord->addSection(); $content = '<p>T

我从tinymce获取html正文,其中包含图像和文本(段落)。但当我试图将其插入模板时,会得到空白图像

这是我的代码:

$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();
}
$phpWord=new\phpooffice\phpWord\phpWord();
$templateProcessor=new\PhpOffice\PhpWord\templateProcessor('./template.docx');
$section=$phpWord->addSection();
$content='Test

'; 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'); 函数getXMLContent($section){ $xmlWriter=newxmlwriter(xmlWriter::STORAGE_MEMORY,'./',Settings::hascability()); $containerWriter=新容器($xmlWriter,$section); $containerWriter->write(); 返回$xmlWriter->getData(); }

我做错什么了吗?或者还有其他方法吗?

首先,您使用的是哪个版本的PHPWord? 如果您使用的是最新版本(截至2020年10月),则可以使用此功能:

将图像添加到Word模板非常简单:

setImageValue(,)

我目前正试图确定图像的高度和宽度,但我发现它有问题,图像宽度固定在3.04厘米

插入图像并设置其高度和宽度的API:


setImageValue(,数组('path'=>,'height'=>,'width'=>);

尝试将其编码为base64格式。请参阅此主题:相同的结果:(