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
In this step, we'll install the laravel 11 application using the following command.
composer create-project laravel/laravel example-app
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
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
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');
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>
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like:
In this article, we will see how to import SQL files into MySQL using the command. You can import databases in multiple...
Nov-10-2022
Hello, laravel web developers! In this article, we'll see how to create a dependent dropdown in laravel 11 Livewire....
Jun-03-2024
In this example, we will see how to get the last record in laravel 8. You can simply get the last record using laravel 8...
Jan-26-2022
In this tutorial we will see laravel 8 datatables keeping selected page number after callback. In datatable page nu...
Dec-03-2021