A Laravel-based application is converting documents (.doc, .docx, .pdf, .png, .otd, html, etc) to PDF so that they can all be merged together into a master PDF document. It is using a combination of plugins like PHPWord and DOMPDF Wrapper to do the file loading and creation. Every once and awhile, the process encounters an error due to a Word file.
ERROR: PhpOffice\PhpWord\Exception\InvalidImageException: Invalid image: zip:// ... #word/media/image2.emf
The error is caused by an image background within the document that acts like a watermark. The PHPWord part that errors out is the PhpOffice\PhpWord\Element\Image->checkImage()
method, but happens when the file is trying to be loaded.
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
$pdfWord = IOFactory::load(storage_path() . '/app/uploads/randomfile.docx', 'Word2007');
How can the application convert a Word document, with an EMF image embedded, to a PDF?
For more code/info on how to recreate the error, a few issues exist in the Github PHPWord library.
The environment-related information:
EDIT:
I also tried to come at this from a different angle, to no avail. I tried using PHP's ZipArchive to unzip the docx file, remove the emf image from the document (ZipArchive::deleteName()
), remove the reference to the emf image in the [Content_Types].xml
(ZipArchive::getFromName()
), then zip the docx file back up but that did not work. I can open the new docx file and see that the image is gone, but the PHPWord error still persists in the application.
It looks like PHPWord has a feature request open to solve this issue.
https://github.com/PHPOffice/PHPWord/issues/1480
I think you're on the right path with the file alteration, there is probably a reference to the image you are missing somewhere that PHPWord is still trying to access.
I would unzip the file on your local drive and grep (search the contents of the exploded file) the directory for the file you are looking for. This will show you where else you may need to remove it from being referenced in the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With