Reading a Word document with images using php

I have a piece of code that I use to read MS Office Word documents.

It reads only text not all content.

<?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 = "customers.docx";

$content = read_file_docx($filename);
if($content !== false) {

    echo nl2br($content);   
}
else {
    echo 'Couldn\'t the file. Please check that file.';
}

?>

I want to read images, graphics, and all content, as well as display them on a web page.

+4
source share
4 answers

I think you should first modify the document documents in pdf using the command line Open Office or Libre Office.

with Libre Office it will be:

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

and then use pdf.js ( https://github.com/mozilla/pdf.js/ ) to display documents on your site (you don't need Adobe Reader)

https://github.com/vivin/pdfjs-text-selection-demo ( minimum.js, , pdf)

- doc docx https://github.com/stephen-hardy/DOCX.js

+2

-, Microsoft.


MS Word, , .

, , MS Office PHP.

+1

You must check the Aspect Cloud . This is a service that allows you to convert docx to html

Github has a PHP SDK for it.

There is a free option if you convert less than 100 documents per month

luck

+1
source

All Articles