首页 > 解决方案 > 如何在 laravel 中使用 phpword 将 html/php 代码转换为 doc

问题描述

我正在使用 Phpword 在 laravel 中创建 doc 文件。下面的代码工作正常。

    public function downloaddoc(){
    $wordTest = new \PhpOffice\PhpWord\PhpWord();

    $newSection = $wordTest->addSection();

    $desc1 = "The Portfolio details is a very useful feature of the web page. You can establish your archived details and the works to the entire web community. It was outlined to bring in extra clients, get you selected based on this details.";

    $newSection->addText($desc1, array('name' => 'Tahoma', 'size' => 15, 'color' => 'red'));

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

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

但我想将一些 html/php 代码转换为 doc 文件。它可以像我们在 pdf 中那样将 php 文件转换为 doc 文件吗?如果是,那么如何。如果不是比什么是替代解决方案。?

标签: phphtmllaravelphpword

解决方案


我相信这可以通过Heredoc 语法来完成

这样做不会解析其中的 PHP 语法。

<?php
$code = <<<EOT
<?php
$var = "string";
?>
EOT;
echo $code;
?>

将按原样输出:

<?php
 $var = "string";
?>

推荐阅读