Support the ongoing development of →
posted 9 years ago
Last updated 1 year ago.

If you need basic stuff, you can do it like this:

$headers = array(

$content = '<html>
			<meta charset="utf-8">
					My Content

return Response::make($content,200, $headers);

If you need more complex stuff, I recommend PHPOffice/PHPWord

Last updated 1 year ago.

and could give me a basic example using PHPOffice/PHPWord

Last updated 1 year ago.

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:
	    array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
	$section->addText('Hello world! I am formatted by a user defined style',

	// You can also put the appended element to local object like this:
	$fontStyle = new \PhpOffice\PhpWord\Style\Font();
	$myTextElement = $section->addText('Hello World!');

	// Finally, write the document:
        // The files will be in your public folder
	$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');

	$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');

	$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF');


There is too much info you have to look. Please take a look at documentation

Last updated 1 year ago.

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 1 year ago.

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

Last updated 1 year ago.

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 1 year ago.

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

Last updated 1 year ago.

mgsmus you had every reason

Last updated 1 year ago.

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 1 year ago.

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!


Your banner here too?

montes2012 montes2012 Joined 11 Feb 2014


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

Your logo here?

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

© 2024 - All rights reserved.