Content Overview
- Step 1: Download Laravel 5.7
- Step 2: Install laravel-dompdf Package
- Step 3: Create Routes
- Step 4: Create Controller
- Step 5: Create Blade File
Step 1: Download Laravel 5.7
I am going to explain step by step from scratch so, we need to get fresh Laravel 5.7 application using bellow mand, So open your terminal OR mand prompt and run bellow mand:
poser create-project --prefer-dist laravel/laravel blog
Step 2: Install laravel-dompdf Packagefirst of all we will install barryvdh/laravel-dompdf poser package by following poser mand in your laravel 5.7 application.
poser require barryvdh/laravel-dompdf
After successfully install package, open config/app.php file and add service provider and alias.config/app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 3: Create RoutesIn this is step we need to create routes for items listing. so open your "routes/web.php" file and add following route.
routes/web.php
Route::get('generate-pdf','HomeController@generatePDF');
Step 4: Create ControllerHere,we require to create new controller HomeController that will manage generatePDF method of route. So let's put bellow code.
app/Http/Controllers/HomeController.php
php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class HomeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function generatePDF()
{
$data = ['title' => 'Wele to HDTuto.'];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('itsolutionstuff.pdf');
}
}
Step 5: Create Blade FileIn Last step, let's create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:
resources/views/myPDF.blade.php
Wele to ItSolutionStuff. - {{ $title }}
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea modo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Now we are ready to run this example and check it...
I hope it can help you...