douzhang1299 2017-09-15 17:42
浏览 103
已采纳

laravel 5.4,phpword:如何在不强制下载的情况下显示具有只读权限的单词doc?

I'm new in laravel and I use phpword to edit a word doc file.

I want once I save it I can display it in read only permission.

I want to display it directly without forcing download.

I tried this code but it forces the download.

Here is my code :

  public function create()
  {
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();
    //Ajouter l'image
    $section->addImage(
      'C:\wamp\www\Stage_2\public\images\mobilis256.png',
      array(
        'width'         => 100,
        'height'        => 100,
        'marginTop'     => -1,
        'marginLeft'    => -1,
        'wrappingStyle' => 'behind'
        ));
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    try {
      $objWriter->save(storage_path('helloWorld.docx'));
    }catch(Exception $e)
    {}
    $filename = 'helloWorld.docx';
    $path = storage_path($filename);
    return Response::make(file_get_contents($path), 200, [
      'Content-Type' => 'application/docx',
      'Content-Disposition' => 'inline; filename="'.$filename.'"'
      ]);
  }

i tried also

return response()->file(storage_path('helloWorld.docx'));

but always the same result.

what should I do, and how can i disply it in read only permission?

  • 写回答

1条回答 默认 最新

  • dongwenyou4298 2017-09-18 10:44
    关注

    I got the solution finally so what I did was:

    I saved the document as html file like this :

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

    Then using dompdf : https://github.com/barryvdh/laravel-dompdf I convert the html file to pdf :

     return PDF::loadFile(storage_path('helloWorld.html'))->save(storage_path('helloWorldPdf.html'))->stream('download.pdf');
    

    So like this I can preview the file without forcing the downloading and to not redo the work.

    The final code look like this:

      use PDF;
      public function create()
      {
      $phpWord = new \PhpOffice\PhpWord\PhpWord();
      $section = $phpWord->addSection();
      //Ajouter l'image
      $section->addImage(
      'C:\wamp\www\Stage_2\public\images\mobilis256.png',
      array(
      'width'         => 100,
      'height'        => 100,
      'marginTop'     => -1,
      'marginLeft'    => -1,
      'wrappingStyle' => 'behind'
      ));
      // Saving the document as HTML file...
      $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
      try {
      $objWriter->save(storage_path('helloWorld.html'));
      }catch(Exception $e)
      {}
      return PDF::loadFile(storage_path('helloWorld.html'))->save(storage_path('helloWorldPdf.html'))->stream('download.pdf');
      }
    

    Just if you want to use domppdf to do this conversion do not do that :

    After updating composer add the following lines to register provider in >bootstrap/app.php

    $app->register(\Barryvdh\DomPDF\ServiceProvider::class);

    To change the configuration, copy the config file to your config folder and >enable it in bootstrap/app.php:

    $app->configure('dompdf'); Here the explication of the kind of error you will get . https://github.com/barryvdh/laravel-dompdf/issues/192

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀