10-Apr-2023
.
Admin
Hi Guys
In this tutorial, I will show the how to 7/ 6 generate pdf file in laravel 7/ 6.we will explain to the generate pdf file using dompdf package .we can generate pdf file from view or html blade file in laravel 7/ 6.
PDF is most using in level project. PDF is one of basic requirement when you are working with erp level project or e commerce website. we may need to create pdf file for report or invoice etc
Here i give you full example of generate pdf file example step by step like create laravel 7/ 6 project,controller, route, blade file etc. So you have to just follow few steps.
Step 1 : Install Laravel 7/ 6 Application
We are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step: 2 Install dompdf Package
Here in this step the dompdf Package
composer require barryvdh/laravel-dompdf
Add providers and aliases
After the install package add providers and aliases in the "config/app.php" file.
following path: config/app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3: Create Route
In this is step we need to create route for generate pdf layout file
following path:/routes/web.php
Route::get('pdf-generate','PdfGenerateController@PDFgenerate');
Step 4: Create Controller
Here this step now we should create new controller as PdfGenerateController,So run bellow command for generate new controller
php artisan make:controller PdfGenerateController
now this step, this controller will manage Generate PDF File layout bellow content in controller file.following fille path
following path:/app/Http/Controllers/PdfGenerateController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PdfGenerateController extends Controller
{
public function PDFgenerate()
{
$data = ['title' => 'NiceSnippets Blog'];
$pdf = PDF::loadView('ourPDF', $data);
return $pdf->download('Nicesnippets.pdf');
}
}
Step 5: Create View File
In Last step, let's create ourPDF.blade.phpfor layout of pdf file and put following code
following path:/resources/views/myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
<title>NiceSnippets</title>
</head>
<body>
<h1>Welcome to Nicesnippets.com - {{ $title }}</h1>
<p>NiceSnippets 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>
Now we are ready to run our example so run bellow command for quick run:
php artisan serve
Now you can open bellow URL on your browser:
http://localhost:8000/pdf-generate
Preview
It will help you...
#Laravel 7
#Laravel
#Laravel 6