使用 php 阅读带有图像的 Ms Word 文档


Read Ms word document with images using php

我有一大块代码用于阅读MS Office Word文档。

它只阅读文本而不是所有内容。

<?php
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);
    }
    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;
}

$filename = "客户.docx";

$content = read_file_docx($filename);
if($content !== false) {
    echo nl2br($content);   
}
else {
    echo 'Couldn''t the file. Please check that file.';
}
?>

我想阅读图像,图表和所有内容,并将其显示在网页中。

我认为您应该首先使用命令行将文档文档更改为pdf 打开办公室或自由办公室。

使用自由办公室,它将是:

libreoffice --headless --convert-to pdf your_file_name.doc

然后使用 PDF.js ( https://github.com/mozilla/pdf.js/) 在您的网站上显示文档(您不需要 Adobe Reader)

这是另一个最小的例子https://github.com/vivin/pdfjs-text-selection-demo(阅读最小.js文件以了解pdf是如何插入的)

第二种选择是将 doc 转换为 docx 并使用 https://github.com/stephen-hardy/DOCX.js

如果您尝试自己提取所有文档内容并转换为匹配的Web显示,我建议您按Microsoft阅读格式规范。

  • Office Open XML是ISO/IEC和ECMA标准(ECMA甚至免费提供文档),可以在这里找到:http://www.ecma-international.org/publications/standards/Ecma-376.htm

  • "旧"二进制 Office 格式可直接从Microsoft获得:http://msdn.microsoft.com/en-us/library/cc313118.aspx


如果您只是在寻找一种从MS Word文档中提取内容的便捷方法,我强烈建议您研究已经处理文档处理和提取的库。

我知道有2个项目正在用PHP处理MS Office文档。

  • PHPOffice/PHPWord(我不确定该项目的Word分支发展了多远。该项目起源于仅支持MS Excel的较小规模,但他们现在也在开发Word和PowerPoint)

  • PHPDocX(这是一个拆分项目。您可以获得具有基本功能集的 LGPL 许可版本或商业付费版本,该版本应该可以处理您在常见 Word 文档中找到的大多数内容)

你应该看看Aspose Cloud。这是一项允许您将docx转换为html的服务

github上有一个PHP SDK。

如果您每月转换的文档少于 100 个,则有一个免费选项

祝你好运