dongtu1789 2016-06-07 03:26
浏览 164

show doc,docx文件内容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

  • 写回答

2条回答 默认 最新

  • drtwqc3744 2016-07-15 01:05
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起