Raviya Technical
Published in

Raviya Technical

Laravel with Packages | How to create a word document file in Laravel?

This article will provide you with how to create a word document file in laravel. in this post, I want to share with you a simple example of how to generate a word file in laravel. you just need to follow a few steps to create a word file in laravel 6, laravel 7, laravel 8, and laravel 9.

In today's world, we are sometimes required to create a word document to 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, you can do it using phpoffice/phpword composer package.

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

So let’s see the below steps.

Step 1: Install Phpword Package

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

composer require phpoffice/phpword

Step 2: Add Route

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

routes/web.php

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

Step 3: Add Controller Method

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

app/Http/Controllers/HomeController.php

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use Exception;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function generateDocx()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$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.";
$section->addImage("https://via.placeholder.com/150?text=raviyatechnical");
$section->addText($description);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
try {
$objWriter->save(storage_path('helloWorld.docx'));
} catch (Exception $e) {
}
return response()->download(storage_path('helloWorld.docx'));
}
}

I hope it can help you…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store