#LaravelWordfile
Explore tagged Tumblr posts
codesolutionsstuff · 3 years ago
Text
How To Create Word Document File In Laravel
Tumblr media
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. Create Word File in Laravel
Create Word File in Laravel
@csrf Name: Email: Phone Number: Submit
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 IlluminateHttpResponse */ 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 PhpOfficePhpWordPhpWord(); $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 = PhpOfficePhpWordIOFactory::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 PhpOfficePhpWordPhpWord(); $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 = PhpOfficePhpWordIOFactory::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 PhpOfficePhpWordPhpWord(); $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 = PhpOfficePhpWordIOFactory::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. Read the full article
0 notes