Laravel Create Word Document File using phpoffice/phpword package

Laravel Create Word Document File using phpoffice/phpword package

In this post we will give you information about Laravel Create Word Document File using phpoffice/phpword package. Hear we will give you detail about Laravel Create Word Document File using phpoffice/phpword packageAnd how to use it also give you demo for it if it is necessary.

Today, i am going to share with you how to generate docx file using phpword in laravel 5 application. After read this tutorial you can understand how to use phpword with laravel 5, laravel 5.1, laravel 5.2, laravel 5.3, laravel 5.4 and also upcoming laravel 5.5 version.

In todays world, we sometimes require to create word document for export some data. For example if you have some important data like terms and conditions then it’s always want to pdf or word that way we can get in better formate, So in laravel 5 you can do it using phpoffice/phpword composer package.

In this example, i will give you very simple example to generate docx file. Here i installed phpoffice/phpword package and then i simply create one route, then in controller method i add simple text and one image. So you can basically understand how it works.

So let’s see bellow steps.

Step 1: Install Phpword Package

Here, in first step we will install phpoffice/phpword package, this package is not specially for laravel but it is for php, but anyway we can use normally, So let’s simple run bellow command and install it:

composer require phpoffice/phpword

Step 2: Add Route

After installed successfully phpword package, we will add new route for demo to generate docx file, So let’s add one route for demo.


Route::get('generate-docx', 'HomeController@generateDocx');

Also see:Laravel 5 import export to excel and csv using maatwebsite example.

Step 3: Add Controller Method

In last step, we will add generateDocx() method in HomeController. So i give you whole code of my controller, let’s copy and paste bellow code:


Also see:Laravel 5 – create quick backend admin panel tutorial


namespace AppHttpControllers;

use IlluminateHttpRequest;

use Exception;

class HomeController extends Controller



* Show the application dashboard.


* @return IlluminateHttpResponse


public function generateDocx()


$phpWord = new PhpOfficePhpWordPhpWord();

$section = $phpWord->addSection();

$description = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";



$objWriter = PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');

try {


} catch (Exception $e) {


return response()->download(storage_path('helloWorld.docx'));



You can easily run above example and check it. You can also get more information about phpword from here : phpword.

I hope it can help you…

Hope this code and post will helped you for implement Laravel Create Word Document File using phpoffice/phpword package. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

84  +    =  93

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US