6

I am passing data from my controller to my blade file, and then want to export the blade file to word document, so far things are controlled, I can export blade to word document, the issue is the document is not opening in Microsoft word it says "word found unreadable content in 1.docx". Below is the code that I am using

$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);
  • I am passing data to blade and then storing all information in a variable
  • creating a file name for reference
  • creating headers to be used when downloading file
  • making the response with blade content variable and header information

Any help will be appreciated

kkarayat
  • 392
  • 1
  • 5
  • 14

5 Answers5

5

You could render your blade to HTML first, then use something like phpdocx to convert that HTML to a Word document.

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
4

This is part of my old project that loads the result of some view as a msword document. Of course you can change the style section as you want

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>
splash58
  • 26,043
  • 3
  • 22
  • 34
  • my html has a table with nested table inside it and that is why when the word document generates, it shows xml error in microsoft word, for open office it is okay, do you have any solution for that ?? – kkarayat Aug 28 '19 at 11:22
  • In that case html has some tables without any problem. But I'm not sure about nested. Really i don't make xml, just give html to msword to interpret it – splash58 Aug 28 '19 at 11:26
  • I have done the same created html with table passed it to msword for interpreting, but it says "XML Error" – kkarayat Aug 28 '19 at 14:22
  • @kkarayat can you put anywhere something as result of `var_export($view);` because its impossible to reproduce the problem – splash58 Aug 28 '19 at 14:28
2

I would try something like this:

<?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>";
?>
Logan Craft
  • 589
  • 4
  • 9
0

Here is the demonstration of how to accomplish it.

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>  

Controller/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"];
}
Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
0

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);
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58