How to Convert Word Docx to PDF in Laravel 11

Websolutionstuff | Jul-01-2024 | Categories : Laravel

Hello, laravel web developers! In this article, we'll see how to convert Word doc to PDF in laravel 11. In laravel 11, we'll convert DOCX to PDF. Here, we'll use barryvdh/laravel-dompdf and phpoffice/phpword package.

phpoffice/phpword is a pure PHP library for reading and writing word-processing documents. Dompdf is an HTML to PDF converter.

Laravel 11 Convert Word Docx to PDF

laravel 11 convert word docx to pdf

 

Step 1: Install Laravel 11 Application

In this step, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel example-app

 

Step 2: Install Composer Packages

Then, we'll install barryvdh/laravel-dompdf and phpoffice/phpword packages using the following composer command.

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

 

Step 3: Create Controller

Next, we'll create a controller using the following command.

php artisan make:controller WordToPDFConvertController

app/Http/Controllers/WordToPDFConvertController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class WordToPDFConvertController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        return view('word-to-pdf');
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(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));
    }
}

Note: create an upload folder with the necessary permissions

 

Step 4: Define Route

Then, we'll define the routes in the web.php file.

routes/web.php

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

 

Step 5: Create View File

Next, we'll create a word-to-pdf.blade.php file

<!DOCTYPE html>
<html>
<head>
    <title>How to Convert Word Docx to PDF in Laravel 11 - Websolutionstuff</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>How to Convert Word Docx to PDF in Laravel 11 - Websolutionstuff</h2>
      </div>
    
      <div class="panel-body">
          
        <form action="{{ route('word.pdf.store') }}" 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</button>
            </div>
        
        </form>
        
      </div>
    </div>
</div>
</body>
      
</html>

 

Step 6: Run the Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve

 


You might also like:

Recommended Post
Featured Post
How To Import SQL File Into MySQL Using Command
How To Import SQL File Into My...

In this article, we will see how to import SQL files into MySQL using the command. You can import databases in multiple...

Read More

Nov-10-2022

Laravel 11 Livewire Dependent Dropdown
Laravel 11 Livewire Dependent...

Hello, laravel web developers! In this article, we'll see how to create a dependent dropdown in laravel 11 Livewire....

Read More

Jun-03-2024

How To Get Last Record In Laravel 8
How To Get Last Record In Lara...

In this example, we will see how to get the last record in laravel 8. You can simply get the last record using laravel 8...

Read More

Jan-26-2022

Laravel 8 Datatables Keeping Selected Page Number
Laravel 8 Datatables Keeping S...

In this tutorial we will see laravel 8 datatables keeping selected page number after callback. In datatable page nu...

Read More

Dec-03-2021