How to Convert Word Docx to PDF in Laravel - TechvBlogs

How to Convert Word Docx to PDF in Laravel

Discover a seamless approach to converting Word Docx files to PDF in Laravel. Elevate your document management game with practical insights and step-by-step guidance in this comprehensive tutorial.

Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya

2 months ago

TechvBlogs - Google News

I'd like to guide you through the process of converting a Word document to PDF in Laravel. This example demonstrates a straightforward method of converting a Word DOCX file to a PDF within the Laravel framework. Get ready to explore how to achieve this conversion seamlessly in Laravel, gaining insights into the process of converting DOCX to PDF using Laravel.

If you need assistance with converting a Word file to PDF in Laravel, I'm here to provide a step-by-step guide. We'll utilize two composer packages, namely barryvdh/laravel-dompdf and phpoffice/phpword, to facilitate the conversion. Let's proceed by following the outlined steps to complete this example.

You can perform Word to PDF conversion in Laravel across versions 6, 7, 8, 9, and 10 using the same approach.

Step 1: Install Laravel

While this step is optional, if you haven't created the Laravel app yet, feel free to proceed by executing the following command:

composer create-project laravel/laravel example-app

Step 2: Install Composer Packages

Next, we will install the barryvdh/laravel-dompdf and phpoffice/phpword packages using the following Composer command. Execute the command below:

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

Step 3: Create Controller For Converting Word Docx to PDF

In this step, we will create a WordToPDFController with index() and store() methods, where we will write the code to generate the PDF.

Ensure that the uploads folder with the necessary permissions.

So, let's create the controller using the following command:

php artisan make:controller WordToPDFController

Now, update the code in 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 the routes/web.php file and update the code in 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


<!DOCTYPE html>
    <title>Laravel Word Dox File to PDF File Example -</title>
    <link href="[email protected]/dist/css/bootstrap.min.css" 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 necessary steps have been completed. Now, type the command below and hit enter to run the Laravel app:

php artisan serve

Now, open your web browser, enter the provided URL, and view the output of the app:


Thank you! If you have any more questions or if there's anything else I can assist you with, feel free to let me know.

Comments (0)


Note: All Input Fields are required.