使用phpword createReader从docx文件中提取内容

时间:2018-03-03 06:36:10

标签: laravel phpword

我想在上传时从docx文件中提取内容但我收到此错误:类PhpOffice \ PhpWord \ PhpWord的对象无法转换为字符串 任何解决方案请告诉我?

public function store(Request $request){

        $this->validate($request, [
             'file'=>'required|mimes:doc,pdf,docx'
         ]);

      if($request->hasFile('file')) {
           $file= $request->file('file');
            $fileName = time() .'.'.$file->getClientOriginalExtension();
               $phpWord = new \PhpOffice\PhpWord\PhpWord();

            $obj = \PhpOffice\PhpWord\IOFactory::createReader($phpWord, 'Word2007');
            $obj->load($fileName);

            $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($obj, 'Word2007');
            try {
             $objWriter->save(storage_path('my.docx'));
            } catch (Exception $e) {

    }
             return response()->download(storage_path('my.docx'));

 }

}

1 个答案:

答案 0 :(得分:0)

<强>答案:

public function store(Request $request){


  $this->validate($request, [
    'file'=>'required|mimes:docx,doc'
  ]);
  $fatwa = new Fatwa;
  if($request->hasFile('file')) {
    $file= $request->file('file');


    $objReader= \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
    $phpWord= $objReader->load($file);

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    try {
      $objWriter->save(storage_path('my.docx'));
    } catch (Exception $e) {

    }
    return response()->download(storage_path('my.docx'));

  }

}