dompdf
package for generating the PDF file. barryvdh/laravel-dompdf
using composer package and thereafter we will add new route url with controller. Then we will create a blade file. Then after we have to just run project with serve and we can check the PDF file is for download. dompdf
. To get started, we need to download fresh Laravel 5.7 application using command, so open our terminal and run the below command in the command prompt: composer create-project --prefer-dist laravel/laravel blog
composer require barryvdh/laravel-dompdf
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Route::get('demo-generate-pdf','HomeController@demoGeneratePDF');
generatePDF()
method of route. <?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class HomeController extends Controller
{
public function demoGeneratePDF()
{
$data = ['title' => 'Welcome to My Blog'];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('demo.pdf');
}
}
<!DOCTYPE html>
<html>
<head>
<title>Hi</title>
</head>
<body>
<h1>Welcome to My BLOG - {{ $title }}</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
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.</p>
</body>
</html>
php artisan serve
SPONSORS