问题
$phpWord = new PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, '<table style="width:100%"><tr><td><img src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg" style="width: 20px;"/></td><td>text</td></tr></table>');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($docx);
In this case PhpWord display image with full size, and ignore image width
How fix?
回答1:
The HTML parser does not seem to understand the css styling put on . What does work is to pass the width as an attribute
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, '<table style="width:100%"><tr><td><img src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg" width="200"/></td><td>text</td></tr></table>');
The code above works for me. At least with 0.14, and opening the word document with Word 2016.
来源:https://stackoverflow.com/questions/48417365/phpword-and-images-with-width