Felix
Felix

Reputation: 581

show doc,docx file content php

I have problem that showing the content or docx file in php(laravel 5).
I did not find any solution to show the content. I use phpword lib for reading, I read the document of phpword but not find the solution.
Here is my code:
+upload in html:

<form method="post" action="doc/upload" enctype="multipart/form-data">
            {{csrf_field()}}
            <input type="file" name="file" accept=".doc, .docx"/>

            <button class="btn btn-primary" style="margin-top: 5px"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Upload</button>
        </form>

+ process:

public function upload(Request $request){
    $file = $request->file('file');
    $phpWord = \PhpOffice\PhpWord\IOFactory::load($file);
    //i use this line below for showing but it can not show exactly 
    $phpWord->save('php://output');
}

+result:
enter image description here

Upvotes: 1

Views: 11075

Answers (4)

Govind Totla
Govind Totla

Reputation: 1178

From the Rabb-bit's answer, The loop was not working for me so this may help. We just need to load the file and then can get the content without formatting from a doc/Docx file.

$phpWord = IOFactory::load( $filePath );

foreach($phpWord->getSections() as $section) {
        foreach($section->getElements() as $element) {
            if (method_exists($element, 'getElements')) {
                foreach($element->getElements() as $childElement) {
                    if (method_exists($childElement, 'getText')) {
                        $content .= $childElement->getText() . ' ';
                    }
                    else if (method_exists($childElement, 'getContent')) {
                        $content .= $childElement->getContent() . ' ';
                    }
                }
            }
            else if (method_exists($element, 'getText')) {
                $content .= $element->getText() . ' ';
            }
        }
    }

Upvotes: 0

JasonJensenDev
JasonJensenDev

Reputation: 2407

For those who are looking for a simpler (and in my case, much more accurate) way of converting a Microsoft Word document to plain text, I would recommend taking a look at this thread found here: How to extract text from word file .doc,docx,.xlsx,.pptx php

Upvotes: 1

Rabb-bit
Rabb-bit

Reputation: 835

I was also looking for the answer for this and here's my code to read a docx file using PHPWord. I'm completing the steps from installation (this is for laravel 5)

Step 1. add https://github.com/PHPOffice/PHPWord in your composer file. (composer.json)

"require": {
       "phpoffice/phpword": "v0.13.*"
    }

Step 2. Add to your controller the IOFactory.

use PhpOffice\PhpWord\IOFactory;

Step 3. create a reader and load the file.

$phpWord = IOFactory::createReader('Word2007')->load($request->file('file')->path());

Step 4. You can already check the $phpWord for properties and contents of the document.

Step 5. If you want to extract the contents of document. use code below

$phpWord = IOFactory::createReader('Word2007')->load($request->file('file')->path());

foreach($phpWord->getSections() as $section) {
            foreach($section->getElements() as $element) {
                if(method_exists($element,'getText')) {
                    echo($element->getText() . "<br>");
                }
            }
        }

Upvotes: 3

Riz
Riz

Reputation: 6992

If you want to show all word doc contetns, then the best way would be to save the word file as html and then show html contents in an Iframe.

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('doc.html');

if you want to grab just the plain text contents from the doc, you might try something like this

foreach($phpWord->getSections() as $section) {
    foreach($section->getElements() as $element) {
        if(method_exists($element,'getText')) {
            echo($element->getText() . "<br>");
        }
    }
}

Hope this helps.

Upvotes: 2

Related Questions