Laravel Convert Word To PDF Tutorial

Laravel Convert Word To PDF Tutorial

Convert Word docx file to PDF file in Laravel; In this tutorial, you will learn how to convert Word docx file to pdf file in Laravel using dompdf and phpoffice/phpword library.

How to Convert Word To PDF in Laravel 10|9|8

Using the following steps, you can convert Word docx file to a pdf file in Laravel 10|9|8 web apps:

  • Step 1: Create a Laravel project
  • Step 2: Install Required Packages & Configuration
  • Step 3: Create Controller File
  • Step 4: Define Routes
  • Step 5: Create Blade View File
  • Step 6: Test This App

Step 1: Create a Laravel project

First of all, Open your cmd or terminal and run the following command into it to create a new laravel project:

composer create-project laravel/laravel MyApp

Step 2: Install Required Packages & Configuration

Next, run the following command into cmd or terminal to install required packages into your laravel project:

composer require barryvdh/laravel-dompdf
composer require phpoffice/phpword

After installation, you need to configure Laravel to use these packages. Open your config/app.php file and add the service providers and aliases:

'providers' => [
    // ...
    PhpOffice\PhpWord\PhpWordServiceProvider::class,
    PhpOffice\PhpSpreadsheet\SpreadsheetServiceProvider::class,
    Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [
    // ...
    'PhpWord' => PhpOffice\PhpWord\Facades\PhpWord::class,
    'PhpSpreadsheet' => PhpOffice\PhpSpreadsheet\Spreadsheet::class,
],

Step 3: Create Controller File

Now, you need to create a new controller file, so run the following command on terminal or cmd:

php artisan make:controller WordDocxToPDFController

Then visit app/Http/Controllers/ directory and open WordDocxToPDFController.php file and add the following code into it:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class WordDocxToPDFController extends Controller
{
   
    public function index(Request $request)
    {
        return view('wordDocxToPDF');
    }
  
  
    public function save(Request $request)
    {
         $fileName = time().'.'.$request->file->extension();  
         $request->file->move(public_path('uploads'), $fileName);
  
         $domPdfPath = base_path('vendor/dompdf/dompdf');
  
         \PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
         \PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF'); 
         $Content = \PhpOffice\PhpWord\IOFactory::load(public_path('uploads/'.$fileName)); 
         $PDFWriter = \PhpOffice\PhpWord\IOFactory::createWriter($Content,'PDF');
  
         $pdfFileName = time().'.pdf';
         $PDFWriter->save(public_path('uploads/'.$pdfFileName)); 
  
         return response()->download(public_path('uploads/'.$pdfFileName));
    }
}

Step 4: Define Routes

Once you have created controller and methods. Now you need to define routes for that.

So visit routes directory and open web.php. Then add the following routes into it:

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\WordDocxToPDFController;
  
/*
|--------------------------------------------------------------------------
| 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('word-docx-to-pdf', [WordDocxToPDFController::class, 'index']);
Route::post('word-docx-to-pdf', [WordDocxToPDFController::class, 'save']);

Step 5: Create Blade View File

Now, you need to resources/views/ directory and create wordDocxToPDF.blade.php & add the following code into it:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Word Dox File to PDF File Example - tutsmake.com</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
        
<body>
<div class="container">
         
    <div class="panel panel-primary">
    
      <div class="panel-heading">
        <h2>Laravel 10|9|8 Word Dox File to PDF File Example - Tutsmake.com</h2>
      </div>
    
      <div class="panel-body">
          
        <form action="{{ URL('word-docx-to-pdf') }}" method="POST" enctype="multipart/form-data">
            @csrf
    
            <div class="mb-3">
                <strong class="form-label" for="inputFile">Upload Word File:</strong>
                <input 
                    type="file" 
                    name="file" 
                    id="inputFile">
            </div>
     
            <div class="mb-3">
                <button type="submit" class="btn btn-success">Convert Word To PDF</button>
            </div>
        
        </form>
        
      </div>
    </div>
</div>
</body>
      
</html>

Step 6: Test This App

Finally, open your terminal or cmd and run the following command into it to start development server for your laravel project:

php artisan serve

Now, you can convert word docx to pdf in laravel by visiting the following URL in your browser:

http://localhost:8000/word-docx-to-pdf

Conclusion

Congratulations 🎊! You have successfully learned how to convert word docx file to pdf file in Laravel 10|9|8 using Laravel DomPDF.

Recommended Laravel Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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