Hi, I am new to PHPWord and I am struggling with embedded images. The following code works fine if I output to HTML or ODText, but produces a seemingly blank document when using the Word2007, PDF or RTF writers:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addImage(
'/vagrant/tmp/test.jpg',
array(
'width' => 600,
'height' => 402,
)
);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('/vagrant/tmp/'.uniqid().'.docx');
echo 'DONE';
I have also run Sample_13_Images.php, which does the same thing - HTML and ODText output is fine, other formats are not.
The actual filesize is inflated by the size of the image (so it does appear to be embedded) but it does not appear.
I don't see any errors in the Apache log. I am using CentOS 6.4 and PHPWord 0.11.1. PHP-GD and PHP-XML are installed.
Hope I'm not being stupid!
The same issue like this:
https://github.com/PHPOffice/PHPWord/issues/306
Yes, looks very similar. It seems that the RTF and DOCX files do actually embed the images OK, but they don't show up when viewed in certain applications (i.e. Libreoffice). That's a different issue, and I am now using a different method of converting to PDF (PHPWord -> ODT -> PDF via LibreOffice's batch convert) so I till close this issue and let 306 take over.
Thanks for taking the time to respond!
I have found the reason
In file src/PhpWord/Writer/HTML/Element/Image.php
there is statment in line 44:
if (!$parentWriter->isPdf()) {
I have commented it and works perfectly with mpdf
What is the background behind this code?
hubertinio's approach works. if i comment that line out, images in pdf are displayed correctly. what is this code about and how can I circumvent that line without manipulating the library (I am using composer to load the library usually)
Is this being fixed? I need to have images in PDF's and this does not work now (unless fixed as pointed out by @hubertinio )
@hubertinio,
I'm trying to generate PDF from the docx. The images are not getting displayed in the generated PDF file.
I have also tried commenting if (!$parentWriter->isPdf()) { as you said, but still same problem.
PS: I'm using TCPDF.
Most helpful comment
I have found the reason
In file src/PhpWord/Writer/HTML/Element/Image.php
there is statment in line 44:
if (!$parentWriter->isPdf()) {I have commented it and works perfectly with mpdf
What is the background behind this code?