При использовании PHPWord для чтения текстового файла без изображений я не сталкиваюсь с какими-либо проблемами. Как только он содержит изображение, я получаю пустой ответ.
Я пытался с кодом
$source = "./62-curriculum-vitae-saisonnier.docx";
$data = "";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'Word2007');
// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText($data);
// $name=basename(__FILE__, '.php');
$name="docxfile";
$source = __DIR__ . "/results/{$name}.html";
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save($source);
$d = new DOMDocument();
$d->loadHTML(file_get_contents($source));
$body = $d->getElementsByTagName('body')->item(0);
$value = $body->nodeValue;
print_r($body->nodeValue);
Кроме того, я пытался с пользовательским кодом и до сих пор не получил решение.
function read_file_docx($filename) {
$striped_content = '';
$content = '';
if(!$filename || !file_exists($filename)) return false;
$zip = zip_open($filename);
if (!$zip || is_numeric($zip)) return false;
while ($zip_entry = zip_read($zip)) {
if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
if (zip_entry_name($zip_entry) != "word/document.xml") continue;
$content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}// end while
zip_close($zip);
$content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
$content = str_replace('</w:r></w:p>', "\r\n", $content);
$striped_content = strip_tags($content);
return $striped_content;
}