【问题标题】:Laravel blade file to word documentLaravel刀片文件到word文档
【发布时间】:2019-08-03 05:33:04
【问题描述】:

我正在将数据从我的控制器传递到我的刀片文件,然后想将刀片文件导出到 word 文档,到目前为止一切都控制好了,我可以将刀片导出到 word 文档,问题是文档没有打开微软的话它说“在 1.docx 中发现了不可读的内容”。下面是我正在使用的代码

$view = View::make('advertisement.advt_template.template_dr')->with('advtData', $advtData->first())->render();
$file_name = strtotime(date('Y-m-d H:i:s')) . '_advertisement_template.docx';
$headers = array(
            "Content-type"=>"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            "Content-Disposition"=>"attachment;Filename=$file_name"
        );
return response()->make($view, 200, $headers);
  • 我将数据传递给刀片,然后将所有信息存储在一个变量中
  • 创建文件名以供参考
  • 创建下载文件时使用的标头
  • 使用刀片内容变量和标头信息做出响应

任何帮助将不胜感激

【问题讨论】:

  • 你在使用 PHPWord 吗?
  • 是的,用 PHPWord 试过
  • 您可以先将刀片呈现为 HTML,然后使用 phpdocx 之类的工具将该 HTML 转换为 Word 文档。
  • @VikramjitSIngh phpdocx 在付费版本中提供了此功能,目前我正在寻找开源解决方案

标签: php laravel phpoffice


【解决方案1】:

这是我旧项目的一部分,它将某些视图的结果加载为 msword 文档。当然你可以随意改变样式部分

header('Content-Type: application/vnd.msword');
header('Content-Disposition: attachment; filename="test.doc"');
header('Cache-Control: private, max-age=0, must-revalidate');
?>
<head>
<style>
@page {
    size: A4 landscape;
    margin: 1.25cm 2cm 1.5cm 2cm;
}
p { font-size:16px; margin-top:0; padding-top:0 }
table { font-size:14.3px }
h1 { font-size:18.5px; text-align:center;  }
h2 { font-size:16px; text-align:left; margin-bottom:0; padding-bottom:0 }
</style>
</head>
<body>
<!-- Your html -->
</body>
</html>

【讨论】:

  • 我的 html 有一个表,里面有嵌套表,这就是为什么当 word 文档生成时,它在 microsoft word 中显示 xml 错误,对于 open office 没关系,你有什么解决方案??
  • 在这种情况下,html 有一些表格没有任何问题。但我不确定嵌套。真的我不做xml,只是把html交给msword来解释
  • 我已经创建了相同的 html,表将其传递给 msword 进行解释,但它显示“XML 错误”
  • @kkarayat 你能把var_export($view); 的结果放在任何地方吗,因为它不可能重现问题
【解决方案2】:

您可以先将刀片呈现为 HTML,然后使用 phpdocx 之类的东西将该 HTML 转换为 Word 文档。

【讨论】:

  • phpdocx 不是开源的,我正在寻找开源解决方案
【解决方案3】:

我会尝试这样的:

<?php
header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment;Filename=document_name.doc");    
  echo "<html>";
  echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
  echo "<body>";
  echo "<b>My first document</b>";
  echo "</body>";
  echo "</html>";
?>

【讨论】:

    【解决方案4】:

    这里是如何完成它的演示。

    HTML

    <!DOCTYPE html>  
     <html>  
          <head>  
               <title>HTML to Word</title>  
               <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
               <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
               <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
          </head>  
          <body>  
               <br /><br />  
               <div class="container" style="width:800px;">  
                    <br />  
                    <h3 align="center">The Big Title</h3>  
                    <br />  
                    <form method="post" action="ROUTE_HERE">  
                         <label>Enter Title</label>  
                         <input type="text" name="heading" class="form-control" />  
                         <br />  
                         <label>Enter Description in HTML Formate</label>  
                         <textarea name="description" class="form-control" rows="10"></textarea>  
                         <br />  
                         <input type="submit" name="create_word" class="btn btn-info" value="Export to Doc" />  
                    </form>  
               </div>  
          </body>  
     </html>  
    

    控制器/PHP

    public function process(Request $request){
        $heading = $request->input('heading');
        $description = $request->input('description');
    
        header("Content-type: application/vnd.ms-word");  
        header("Content-Disposition: attachment;Filename=".rand().".doc");  
        header("Pragma: no-cache");  
        header("Expires: 0");  
        echo '<h1>'.$_POST["heading"].'</h1>';  
        echo $_POST["description"];
    }
    

    【讨论】:

      【解决方案5】:
      I'm using this and it works form me. You can try it
        $view = view('advertisement.advt_template.template_dr')->with('advtData', 
        $advtData->first())->render();
        $file_name = strtotime(date('Y-m-d H:i:s')) . '_advertisement_template.doc';
        $headers = array(
                  "Content-type"=>"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                  "Content-Disposition"=>"attachment;Filename=$file_name"
              );
      return Response::make($content,200, $headers);
      

      【讨论】:

        猜你喜欢
        • 2013-11-22
        • 2018-12-08
        • 2020-09-28
        • 2016-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-13
        • 2021-08-23
        相关资源
        最近更新 更多