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.

app/Http/Controllers/WordToPDFController.php

<?php

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

\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: Add Route

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

routes/web.php

<?php

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('word.to.pdf.store');

Step 5: Create View File

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

resources/views/wordToPDF.blade.php

<!DOCTYPE html>

<html>

<head>

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

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<div class="panel panel-primary">

<div class="panel-heading">

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

</div>

<div class="panel-body">

<form action="{{ route('word.to.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>

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:

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

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

Layout:

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 :
Shares