How to generate PDF File in laravel ?
Comment (0)
Admin
293
Step 1: Install Laravel Frsh Project
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install dompdf Package
composer 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,
]
Use php artisan vendor:publish to create a config file located at config/dompdf.php which will allow you to define local configurations to change some settings (default paper etc). You can also use your ConfigProvider to set certain keys.
Configuration
The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
Step 3: Add Route
routes/web.php
Route::get('generate-pdf','PDFController@generatePDF');
Step 4: Add Controller
app/Http/Controllers/PDFController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PDFController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function generatePDF()
{
$data = ['title' => 'Welcome to nijwel.xyz'];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('nijwel.pdf');
}
}
Step 5: Create Blade File
resources/views/myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
<title>PDF Generate by nijwel.xyz</title>
</head>
<body>
<h1>Welcome to - {{ $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>
Hope It will help you.......
Comments (0)
Your Comment