Reputation: 392
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);
Any help will be appreciated
Upvotes: 6
Views: 15328
Reputation: 511
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);
Upvotes: 0
Reputation: 9009
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"];
}
Upvotes: 0
Reputation: 18813
You could render your blade to HTML first, then use something like phpdocx to convert that HTML to a Word document.
Upvotes: 5
Reputation: 26153
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>
Upvotes: 4
Reputation: 639
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>";
?>
Upvotes: 2