首页 > 解决方案 > 在 laravel 中使用 PHPWord 预览 Word 文件而不是在浏览器中下载

问题描述

我想在 Laravel 中使用 PHPWord 在浏览器中预览创建的Word文件

我已经成功地制作了一个word文件,但现在我想预览它而不是下载。

我有一个系统,我在创建文件时证明它。PDF 和图像文件工作正常。但是 Word 文件没有在浏览器中获得预览。

这是功能

public function saveAndPreviewDocx()
{
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();

    $description_data = "This is a sample text and i want it to preview this in a browser in word file";       
    $section->addText($description_data);

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    try {
        $objWriter->save(storage_path('new_doc12.docx'));
    } catch (Exception $e) {
    }
 // How can i view the file in the browser form here or form the view using the phpWord

}

此功能正在创建文件,但我想在浏览器中预览创建的文件。

在寻找解决方案的一种方式中,我发现我可以使用 google Docs 视图或 Microsoft Office 视图查看 word 文件,但它们对我没有用,因为我有一台私人服务器。

我已经尝试过Google Docs ViewerMicrosoft Office Viewer,但在我的私人服务器上它不起作用。

因此,当我们看到其他文件(如 PDF 等)时,我想要任何干净的解决方案来解决 Web 浏览器中的这个单词预览。

标签: phplaravelphpwordphpoffice

解决方案


推荐阅读