Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124


Create a dynamic pdf in Laravel, Here I would like to share with you how to create a dynamic PDF file in Laravel. Making a dynamic pdf file is required in many situations like create an invoice , create a report and many more things.
Here in this article we will use Laravel DomPDF Library to generate a dynamic PDF Library.
Follow the below given steps to generate or create pdf in laravel.
| 1. | Install Laravel App |
| 2. | Setup Database |
| 3. | Install laravel-dompdf Package |
| 4. | Make Route |
| 5. | Create Controller |
| 6. | Create Blade view for Generate Pdf |
| 7. | Run Project |
I assume Laravel is already installed, if not follow below link to installation Laravel app
After successfully download Laravel Application,Open your project .env file and set up database credential .
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_demo DB_USERNAME=root DB_PASSWORD=
To create PDF file in Laravel first of all we have to download laravel-dompdf package.
composer require barryvdh/laravel-dompdf
After successfully install the laravel dompdf package, open the config/app.php and add the providers and aliases :
'providers' => [ Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ 'PDF' => Barryvdh\DomPDF\Facade::class, ]
Go to /routes/web.php file and create below routes:
Route::get('pdfview',array('as'=>'pdfview',
'uses'=>'CreatePdfController@pdfForm'));
php artisan make:controller CreatePdfController.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Redirect;
use DB;
use PDF;
class CreatePdfController extends Controller
{
public function pdfForm(Request $request){
$products = DB::table("products")->get();
view()->share('products',$products);
if($request->has('download')){
$pdf = PDF::loadView('pdf_download');
return $pdf->download('pdf_download.pdf');
}
return view('pdf_view',['products'=>$products]);
}
}
Add below code in pdf_view.blade.php file in view folder.
<html>
<head>
<title>Laravel Create & Download Pdf Tutorial</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br/>
<a href="{{ route('pdfview',['download'=>'pdf']) }}" class="btn btn-primary">Download PDF</a>
<table class="table table-bordered">
<tr>
<th>Sr.No</th>
<th>Name</th>
<th>Qty</th>
<th>Price</th>
<th>Model</th>
<th>Sku</th>
</tr>
@foreach ($products as $index=>$product)
<tr>
<td>{{ ++$index }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->qty }}</td>
<td>{{ $product->price }}</td>
<td>{{ $product->model }}</td>
<td>{{ $product->sku }}</td>
</tr>
@endforeach
</table>
</div>
</body>
</html>
add below code in pdf_download.blade.php file in view folder.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pdf Download</title>
</head>
<body>
<table border="1">
<tr>
<th>Sr.No</th>
<th>Name</th>
<th>Qty</th>
<th>Price</th>
<th>Model</th>
<th>Sku</th>
</tr>
@foreach ($products as $index=>$product)
<tr>
<td>{{ ++$index }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->qty }}</td>
<td>{{ $product->price }}</td>
<td>{{ $product->model }}</td>
<td>{{ $product->sku }}</td>
</tr>
@endforeach
</table>
</body>
</html>