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
jQuery After And Before Example
jQuery After And Before Exampl...

In this article I will give you example of jquery after() and before() method. The after() method inserts spec...

Read More

Dec-10-2021

Copy To Clipboard JQuery
Copy To Clipboard JQuery

In this article, we will see how to copy to clipboard jQuery. we will learn how to copy text from textarea&nbs...

Read More

Aug-19-2020

How to Send Bulk Mail Using Queue in Laravel 8
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using a queue in laravel 8. Laravel queue is used for sending bu...

Read More

Feb-10-2021

How to Install Zoom in Ubuntu 22.04 using Terminal
How to Install Zoom in Ubuntu...

Greetings Ubuntu enthusiasts! If you're ready to dive into the world of video conferencing on your Ubuntu 22.04...

Read More

Jan-19-2024