Support the ongoing development of Laravel.io →
posted 10 years ago
Packages
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

and could give me a basic example using PHPOffice/PHPWord

Last updated 2 years ago.
0

Basic Usage Example

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

Last updated 2 years ago.
0

I did what you provided does not work,

composer update

Loading composer repositories with package information Updating dependencies (including require-dev)

  • Installing phpoffice/phpword (dev-master 86369ad) Downloading: 100%

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.

Last updated 2 years ago.
0

Please turn on error detail (set debug true in app/config/app.php) so we can understand what is going on.

Last updated 2 years ago.
0

this is the error:

  1. PhpOffice\PhpWord\Exception\Exception …/­vendor/­phpoffice/­phpword/­src/­PhpWord/­Writer/­Word2007.php122

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."); }

Last updated 2 years ago.
0

It seems PHPWord can't save file. Please check if the folder is writeable.

Last updated 2 years ago.
0

mgsmus you had every reason

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

montes2012 montes2012 Joined 11 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.