How to Generate Word Document File (.doc / .docs ) in Laravel

How to Generate Word Document File (.doc / .docs ) in Laravel

Today in this article we will learn how to generate Word Document File (.doc / .docs ) in Laravel, Many Times we need to generate the HTML file to doc file for export some data from the Laravel application. and we are exporting the file using the PHP function and not using any external package or libraries. the external package or libraries decrease the application performance. There are many websites, they using some PHP libraries like phpword, phpoffice, etc. but in these tutorials, we are generating the word document file with simple PHP code.

Generate Word Document File (.doc / .docs ) in Laravel

We are creating the Word Document File (.doc / .docs ) with the below steps.

Step 1. Create Blade file to Make the Clickable Button

First, we add a button in our laravel blade file for generating the html view page. The button will open the route link of the controller function.

<!DOCTYPE html>
<html>
  <head>
    <title>Generate Word Document File (.doc / .docs ) in Laravel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  </head>
  <body>
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-heading"Generate Word Document File (.doc / .docs ) in Laravel</div>
                    <div class="card-body">
                        <a class="btn btn-primary btn-sm pull-right" href="{{ route('export-the-docx') }}">Download the Docs Files</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
  </body>
</html>

Step 2. Create a controller for process the Document Create process

Now we are creating a controller with the below commands at the root of the project and artisan will create a Controller file for us

php artisan make:controller DocCreateController

After executing the command in the app\Http\Controller\s folder you will find a new file with the name DocCreateController.php

Open the file and enter the function to process the task, we have added Static content but you can add dynamic content and view the file also.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DocCreateController extends Controller
{
   

    /**
     * To show to view so  we can execute the export function with button
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('view-docx');
    }
  
  	/**
     * To create the Docs file from php code
     *
     */
    public function exportDocsFile()
     {
         $filename = 'view-docx.doc';
         header("Content-Type: application/force-download");   //it will download the file without open it in browser
         header( "Content-Disposition: attachment; filename=".basename($filename));
         header( "Content-Description: File Transfer");
         @readfile($filename);

         $docsBody = '<html>
                      <head></head>
                      <body>
                      <h1>This is test heading using docx export.</h1>
                      <p>Hello, its a paragraph for testing text docuents</p>
                      </body>
                      </html>';

          
         echo $docsBody;
      }
}

Step 3. To register the route in web.php so we can check and test the application in browser

Now we need to add the route in the web.php so we can view and export the Document file and check

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DocCreateController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/view-docx', [DocCreateController::class, 'index'])->name('view-docx');  //for view
Route::get('/export-the-docx', [DocCreateController::class, 'exportDocsFile'])->name('export-the-docx'); //for export function

The result View

https://export-the-docs.dev/view-docx

And now the application is ready, I hope you enjoy the tutorials and you learn something new, please give a comment if you have any doubt on it will try to help you.

The Document is made on Laravel 8.x.x with php version 7.4.x so while using the command and function please make sure you are also using the same.

TheTechStuffs Team

You May Also Like

About the Author: Sumanta Kabiraj

Leave a Reply

Your email address will not be published. Required fields are marked *