Hello Devs,
In this tutorial, we are going to learn how to create pdf in laravel 7.
Follow this step by step guide given below:
Step 1 :
Install Laravel 7
composer create-project --prefer-dist laravel/laravel blog
Step 2 :
Install dompdf Package
composer require barryvdh/laravel-dompdf
config/app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3:
Create Route routes/web.php
Route::get('pdf-create','PdfController@create');
Step 4:
Create Controller
php artisan make:controller PdfController
app/Http/Controllers/PdfGenerateController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PdfController extends Controller
{
public function create()
{
$data = ['title' => 'Laravel 7 Generate PDF From View Example Tutorial'];
$pdf = PDF::loadView('pdf', $data);
return $pdf->download('Rathorji.pdf');
}
}
Step 5:
Create View File resources/views/pdf.blade.php
<! DOCTYPE html>
<html>
<head>
<title>Laravel 7 Generate PDF From View Example Tutorial - Rathorji</title>
</head>
<body>
<h1>Welcome to Rathorji.in - {{ $title }}</h1>
<p>Rathorji Blog provides you latest Code Tutorials on PHP, Laravel, Codeigniter,
JQuery, Node js, React js, Vue js, PHP, and Javascript. Mobile technologies like Android,
React Native, Ionic etc.</p>
</body>
</html>
Run this command:
php artisan serve
Open this URL:
http://localhost:8000/pdf-create
I hope this example helps you.