How to Create Word Document File in Laravel?
This article will provide you how to create work document file in laravel. in this post, i want to share with you simple example of how to generate word file in laravel. you just need to follow few step to creating word file in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.
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 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.
routes/web.php
Route::get('generate-docx', 'HomeController@generateDocx');
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:
app/Http/Controllers/HomeController.php
<?php
namespace 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://www.itsolutionstuff.com/frontTheme/images/logo.png");
$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'));
}
}
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel File Manager Tutorial Step by Step
- Laravel Blade Include File Example
- Laravel Scout Algolia Search Example
- Laravel Typeahead Search Example Tutorial
- How to use Soft Delete in Laravel?
- How to Create Pagination from Array in Laravel?
- How to Use Date Format Validation in Laravel?
- How to use whereHas with orWhereHas in Laravel?
- How to Get File Extension from Path in Laravel?
- Laravel Ajax Image Upload with Validation Example
- How to Execute MySQL Query in Laravel?
- How to Get Month Difference Between Two Dates in Laravel?
- How to Increment and Decrement a Column Value in Laravel?
- Laravel Order By with Column Value Example