PHPWord - 无法读取 docx 文件

问题描述 投票:0回答:1

我正在尝试使用 PHPWord 读取 docx 文件,但出现以下错误。我似乎无法在任何地方找到解决方案:

PHP 致命错误:未捕获的异常“PhpOffice\PhpWord\Exception\Exception”,消息为“Word2007” 不是有效的读者。'

这是加载文件的 PHP 代码:

$phpWord = \PhpOffice\PhpWord\IOFactory::load($filepath1); 
$xmlWriter = PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');

任何帮助将不胜感激!

php phpword phpoffice
1个回答
0
投票

Read from docx.file 

function read_docx_file($file_path){

        $striped_content = '';
        $content = '';

        $zip = zip_open($file_path);

        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;
    }

    read_docx_file('example.docx');
    

© www.soinside.com 2019 - 2024. All rights reserved.