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

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

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名