PHPWord is a PHP-only library that provides classes for writing to and reading from various document file formats. PHPWord facilitates Microsoft Office Open XML (OOXML or OpenXML), OASIS Open Document Format for Office Applications (OpenDocument or ODF), Rich Text Format (RTF), HTML, and PDF in its current version. You can also use some simple styles on that document. As is customary, we begin our tutorial by installing Laravel.
Table of Content
- Install Laravel Project
- Install phpoffice/phpword Package
- Build a view file to add the data
- Create one controller and route
- Create Word Document File
In Laravel, create a Word Document File
In Laravel, use the phpoffice/phpword
package to create a word document file. In this example, I’ll show you how to create a word document and then insert text and images into it.
First, we’ll set up the new Laravel Project.
Step 1: Install Laravel Project
Enter the following command in the terminal to download the laravel project.
composer create-project --prefer-dist laravel/laravel laravelworddocument
Step 2: Install phpoffice/phpword Package
By running the following command in cmd, we will install the phpoffice/phpword
package.
composer require phpoffice/phpword
Step 3: Build a view file to add the data
Put the following code in a file called resources >> views >> createdocument.blade.php
.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Create Word File in Laravel</title>
<link rel="stylesheet" href="{{asset('css/app.css')}}">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Create Word File in Laravel</h2><br/>
<form method="post" action="{{url('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>
<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>
<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>
<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
Use the following command to generate the controller.
php artisan make:controller DocumentController --resource
It will generate a single controller file named DocumentController.php
.
In the routes >> web.php
file, we define a route. So let us go ahead and do it.
Route::get('create','DocumentController@create');
Route::post('store','DocumentController@store');
The next process is to go to the DocumentController.php
file and insert a few code into the create() function.
//DocumentController.php
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('createdocument');
}
The Laravel Development server must then be started. So, enter the following command in the terminal.
php artisan serve
Go to your browser and type the following URL: http://localhost:8000/create
Step 5: Create Word Document File
Following that, we can save the data in a word file and download the word file.
Go to the DocumentController.php
file and insert some code into 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/prashant.jpg");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('CodeSolutionStuff.docx');
return response()->download(public_path('CodeSolutionStuff.docx'));
}
If you want to save a document as an ODF file, use the following code.
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/prashant.jpg");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('CodeSolutionStuff.odt');
return response()->download(public_path('CodeSolutionStuff.odt'));
}
If you want to save a document as an HTML file, use the following code.
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/prashant.jpg");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('CodeSolutionStuff.html');
return response()->download(public_path('CodeSolutionStuff.html'));
}
Finally, our How to Create Word Document File in Laravel tutorial has come to an end. Thank you for taking the time.
I hope you will like the content and it will help you to learn How To Create Word Document File In Laravel
If you like this content, do share.