If you need basic stuff, you can do it like this:
$headers = array(
"Content-type"=>"text/html",
"Content-Disposition"=>"attachment;Filename=myfile.doc"
);
$content = '<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>
My Content
</p>
</body>
</html>';
return Response::make($content,200, $headers);
If you need more complex stuff, I recommend PHPOffice/PHPWord
First, install PHPWord via composer:
{
"require": {
"laravel/framework": "4.1.*",
"phpoffice/phpword": "dev-master"
}
}
You can test basic usage example quickly with simple route:
Route::get('test', function() {
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Every element you want to append to the word document is placed in a section.
// To create a basic section:
$section = $phpWord->addSection();
// After creating a section, you can append elements:
$section->addText('Hello world!');
// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.',
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
// If you often need the same style again you can create a user defined style
// to the word document and give the addText function the name of the style:
$phpWord->addFontStyle('myOwnStyle',
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style',
'myOwnStyle');
// You can also put the appended element to local object like this:
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Verdana');
$fontStyle->setSize(22);
$myTextElement = $section->addText('Hello World!');
$myTextElement->setFontStyle($fontStyle);
// Finally, write the document:
// The files will be in your public folder
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF');
$objWriter->save('helloWorld.rtf');
});
There is too much info you have to look. Please take a look at documentation
I did what you provided does not work,
Loading composer repositories with package information Updating dependencies (including require-dev)
phpoffice/phpword suggests installing ext-gd2 (Used to add images) phpoffice/phpword suggests installing dompdf/dompdf (Used to write PDF) Writing lock file Generating autoload files Generating optimized class loader Compiling common classes
I get the following error:
Whoops, looks like something went wrong.
Please turn on error detail (set debug true in app/config/app.php) so we can understand what is going on.
this is the error:
PhpOffice \ PhpWord \ Exception \ Exception Could not close zip file helloWorld.docx.
open: /var/www/html/word/vendor/phpoffice/phpword/src/PhpWord/Writer/Word2007.php
// Close file if ($objZip->close() === false) { throw new Exception("Could not close zip file $filename."); }
It seems PHPWord can't save file. Please check if the folder is writeable.
This solution is good, but what should I put now in app.php file in providers array as a service provider from phpword? It's returning me an error: Class 'PhpWord\PhpWord' not found
Hi, can u give one example of storing database result into word file. I tried to create by seeing documentation but i couldn't. thanks
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community