PDF generation from HTML is a commonly used functionality in web applications. In this post, we'll see how to do this using Laravel PHP.
In laravel, we usually generate PDF files using the barryvdh/laravel-dompdf laravel package. This package is easy to use and offers users several customization options.
Convert HTML to PDF with Laravel PHP
You can easily convert HTML view to PDF, just following the following simple steps:
· Step 1: From a terminal or command prompt, create a Laravel project using composer
composer create-project laravel/laravel htmlpdf |
Create an application key using the command below once the project is created successfully.
php artisan key:generate |
Step 2: Now, use the commands below to install the barryvdh/laravel-dompdf package
composer require barryvdh/laravel-dompdf
|
Now that our barryvdh/laravel-dompdf package has been successfully installed, let's configure it.
Step 3: Configure your barryvdh/laravel-dompdf package by adding the following code to the config/app.php file.
Add the following code in Providers
'providers' => [ .... Barryvdh\DomPDF\ServiceProvider::class, .... ], |
Add the following code in aliases.
'aliases' => [ .... 'PDF' => Barryvdh\DomPDF\Facade::class, .... ],
|
Step 4: You need to configure our PDF settings, including the paper size, DPI, font type, etc. To do so, run the following command, and it will create the config/dompdf.php file
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider" |
Step 5: Now, create a PDF download route by pasting the code below into the routes/web.php file
<?php Route::get('html-pdf', 'HTMLPDFController@htmlPdf')->name('htmlPdf');
|
Step 6: Now that the route has been created, we need to create a controller. Run the command: php artisan make:controller HTMLPDFController
This command will generate the app/Http/Controllers/HTMLPDFController.php
Now open the HTMLPDFController.php file and paste the following code below
<?php
namespace App\Http\Controllers;
use PDF; use Illuminate\Http\Request;
class HTMLPDFController extends Controller { /** * generate PDF file from blade view. * * @return \Illuminate\Http\Response */ public function htmlPdf() { // selecting PDF view $pdf = PDF::loadView('htmlView');
// download pdf file return $pdf->download('pdfview.pdf'); } } |
Now, open app/Http/Controllers and add the following code:
/** * The controller namespace for the application. * * When present, controller route declarations will automatically be prefixed with this namespace. * * @var string|null */ protected $namespace = 'App\\Http\\Controllers'; |
Step 7: Finally, create the view file in the ressource/views/htmlPdf.blade.php and paste the code below. This view contains the HTML document that will be converted to PDF
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>HTML 2 PDF</title> <style type="text/css"> .center { text-align: center; } </style> </head> <body> <div class="center"> <h1>This is heading</h1> </div> Dummy Test here for the flutter example </body> </html> |
We are done. You are now ready to run the php artisan server to run your application.
In this php laravel example we covered how to generate PDF from html content using laravel8
Article Contributed By :
|
|
|
|
871 Views |