AppDividend
Latest Code Tutorials

How to Create Word Document File in Laravel

Create Word Document File in Laravel

0 330

Get real time updates directly on you device, subscribe now.

In this tutorial, we will see How to Create Word Document File in Laravel. We will use phpoffice/phpword package for creating word file. In this example, I will show you how you can generate a word document, and in that document, you can add text and images. PHPWord is a library written in pure PHP that provides a set of classes to write to and read from different document file formats. The current version of PHPWord supports Microsoft Office Open XML (OOXML or OpenXML), OASIS Open Document Format for Office Applications (OpenDocument or ODF), Rich Text Format (RTF), HTML, and PDF. You can also apply some basic styles to that document. As usual, we start our tutorial by installing Laravel first.

How to Create Word Document File in Laravel

First, we install new Laravel Project.

Step 1: Install Laravel Project

Download laravel project by typing following command in terminal.

composer create-project --prefer-dist laravel/laravel laravelworddocument

Step 2: Install phpoffice/phpword Package

We will install phpoffice/phpword package by hit following command in cmd.

composer require phpoffice/phpword

Step 3: Build view file to add the data

Create a file in the resources  >>  views  >>   createdocument.blade.php and put the following code in it.

<!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

Generate the controller using the following command.

php artisan make:controller DocumentController --resource

It will create one controller file called DocumentController.php.

we define  route in routes  >>  web.php file. So let us do it.

Route::get('create','DocumentController@create');
Route::post('store','DocumentController@store');

Next step would be to go to DocumentController.php file and add into create() function some code.

Related Posts
1 of 33
//DocumentController.php

/**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('createdocument');
    }

After that, we need starting 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 DocumentController.php file and add into store() function some 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/Krunal.jpg");  
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('Appdividend.docx');
        return response()->download(public_path('Appdividend.docx'));
    }

If you are saving a document as an ODF file, then add 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.jpg");  
        $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 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.jpg");  
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
        $objWriter->save('Appdividend.html');
        return response()->download(public_path('Appdividend.html'));
    }

 

 

In above screenshot, you can see that we have exported three different formats of the file.

Finally, Our How to Create Word Document File in Laravel is over. Thanks for taking.

Leave A Reply

Your email address will not be published.