To create a Word document file in Laravel, use the “phpoffice/phpword” package. PHPWord is a library written in pure PHP that provides classes to write to and read from different document file formats.
Here are steps by step guide:
Step 1: Install Laravel Project
composer create-project --prefer-dist laravel/laravel laravelworddocument
Step 2: Install phpoffice/phpword Package
We will install the phpoffice/phpword package by hitting the following command in cmd.
composer require phpoffice/phpword
Step 3: Build a view file to add the data
Create a file in the resources >> views >> createdocument.blade.php and put the following code.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Create Word File in Laravel 11</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js" integrity="sha384-0pUGZvbkm6XF6gxjEnlmuGrJXVbNuzT9qBBavbLwCsOGabYfZo0T0to5eqruptLy" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h2>Create Word File in Laravel 11</h2><br/>
<form method="post" action="{{ route('documents.store')}}">
@csrf
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="Name">Name:</label>
<input type="text" class="form-control" name="name">
</div>
</div>
<br />
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="Email">Email:</label>
<input type="text" class="form-control" name="email">
</div>
</div>
<br />
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<label for="Number">Phone Number:</label>
<input type="text" class="form-control" name="number">
</div>
</div>
<br />
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
</body>
</html>
Step 4: Create one controller and route
Generate the controller using the following command.
php artisan make:controller DocumentController --resource
It will create one controller file called DocumentController.php.
We define a route in routes >> web.php file. So let us do it.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DocumentController;
Route::get('/', function () {
return view('welcome');
});
Route::resource('documents', DocumentController::class);
The next step would be to go to the DocumentController.php file and add some into creating () function code.
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('createdocument');
}
After that, we need to start the Laravel Development server. So in the terminal, follow the following command.
php artisan serve
Move to the browser and type this URL:http://localhost:8000/create
Step 5: Create Word Document File
Next, we can save data in a Word file and download the Word file.
Go to the DocumentController.php file and add code to the store() function.
public function store(Request $request)
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$text = $section->addText($request->get('name'));
$text = $section->addText($request->get('email'));
$text = $section->addText($request->get('number'),array('name'=>'Arial','size' => 20,'bold' => true));
$section->addImage("./images/Krunal beard.png");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('Appdividend.docx');
return response()->download(public_path('Appdividend.docx'));
}
Output:
If you are saving a document as an ODT file, then add the following code to save.
public function store(Request $request)
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$text = $section->addText($request->get('name'));
$text = $section->addText($request->get('email'));
$text = $section->addText($request->get('number'),array('name'=>'Arial','size' => 20,'bold' => true));
$section->addImage("./images/Krunal beard.png");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('Appdividend.odt');
return response()->download(public_path('Appdividend.odt'));
}
If you are saving a document as an HTML file, then add the following code to save.
public function store(Request $request)
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$text = $section->addText($request->get('name'));
$text = $section->addText($request->get('email'));
$text = $section->addText($request->get('number'),array('name'=>'Arial','size' => 20,'bold' => true));
$section->addImage("./images/Krunal beard.png");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('Appdividend.html');
return response()->download(public_path('Appdividend.html'));
}
In the above screenshot, you can see that we have exported three different file formats. That’s it for this tutorial. Thanks for taking it.
Daniel Pareja
I can use a view (like the example “createdocument”) to create my word Document?
You only use de view to show in PC, but no to create de docx file.
Daniel Pareja
And I can´t use my view to create the docx file?
Leonardo
Thanks from Chile!!!,
It is the only excellent example, which does not put the folder in C: \ Xampp and download a vendor folder where it generates the html of that word, and complicates for those of us who work with github version control, because at least I do not work in C: / Xampp / htdocs, and when moving the project folder I had to create a whole new repository to upload the project, and it was not the idea, very much thanks, God bless you.
fawad
Thank you very much for your tutorial. I deploy on local web app.