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
 

4 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.

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

resources/views/wordToPDF.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Word Dox File to PDF File Example - TechvBlogs.com</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>Laravel Word Dox File to PDF File Example - TechvBlogs.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 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:

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

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)

Comment


Note: All Input Fields are required.