php - 如何在 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 文件吗?如果是,那么如何。如果不是比什么是替代解决方案。?
解决方案
我相信这可以通过Heredoc 语法来完成
这样做不会解析其中的 PHP 语法。
<?php
$code = <<<EOT
<?php
$var = "string";
?>
EOT;
echo $code;
?>
将按原样输出:
<?php
$var = "string";
?>
推荐阅读
- node.js - 如何为现有数据库构建 Graphql 后端服务器?
- c++ - C++ 20 工作草案是否包含别名导入的模块?
- javascript - 反应带有未定义属性的选项卡
- scala - 什么使递归函数是有限的
- apache - VirtualHost 允许 www 和非 www
- node.js - winston.debug 在终端上不显示任何内容
- java - 无法在 py4j 中从 Python 启动网关
- javascript - 如何处理以快递方式发送到服务器的数据?
- mysql - MySQL ORDER BY 来自同一多个表的多个列
- memory-management - 关于与数据结构相关的地址和参数