How to Convert Word Docx to PDF in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hello Developer,

I'd like to demonstrate how to convert a Word document to PDF in Laravel. This is a straightforward example of converting a Word DOCX file to a PDF within the Laravel framework. I'm excited to show you how to achieve this conversion in Laravel. You'll gain insights into converting DOCX to PDF using Laravel.

If you require assistance with converting a Word file to PDF in Laravel, I'm here to guide you through the process step by step. We'll make use of two composer packages: barryvdh/laravel-dompdf and phpoffice/phpword to achieve this conversion. Let's proceed by following the steps outlined below to complete the example.

you can convert word docs to pdf in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions as well.

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install Composer Packages

next, we will install the barryvdh/laravel-dompdf and phpoffice/phpword package using the following composer command, let's run the below command:

composer require barryvdh/laravel-dompdf

composer require phpoffice/phpword

Step 3: Create Controller

In this step, we will create WordToPDFController with index() and store() where we write the code of generate pdf.

Make sure to create "uploads" folder with permission.

so let's create controller using bellow command.

php artisan make:controller WordToPDFController

Now, update the code on the controller file.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WordToPDFController extends Controller



* Write code on Method


* @return response()


public function index(Request $request)


return view('wordToPDF');



* 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');



$Content = \PhpOffice\PhpWord\IOFactory::load(public_path('uploads/'.$fileName));

$PDFWriter = \PhpOffice\PhpWord\IOFactory::createWriter($Content,'PDF');

$pdfFileName = time().'.pdf';


return response()->download(public_path('uploads/'.$pdfFileName));



Step 4: Add Route

Furthermore, open routes/web.php file and update code on it.



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\WordToPDFController;



| 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', [WordToPDFController::class, 'index']);

Route::post('word-to-pdf', [WordToPDFController::class, 'store'])->name('');

Step 5: Create View File

In Last step, let's create wordToPDF.blade.php for layout of pdf file and put following code:


<!DOCTYPE html>



<title>Laravel Word Dox File to PDF File Example -</title>

<link href="" rel="stylesheet">



<div class="container">

<div class="panel panel-primary">

<div class="panel-heading">

<h2>Laravel Word Dox File to PDF File Example -</h2>


<div class="panel-body">

<form action="{{ route('') }}" method="POST" enctype="multipart/form-data">


<div class="mb-3">

<strong class="form-label" for="inputFile">Upload Word File:</strong>






<div class="mb-3">

<button type="submit" class="btn btn-success">Convert</button>








Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:


you will download sample word file from here: Download Sample Word File


You will able to download following pdf file:

Now we are ready to run this example and check it...

I hope it can help you...

Tags :