Exporting PDF by the web application is an important feature. We need PDF of the customer list, invoice, balance sheet or any other data report. By Laravel and barryvdh/laravel-dompdf package, we can easily generate dynamic PDF from our application. DomPDF can make HTML to PDF and it supports CSS 2.1 compliant HTML layout. The laravel dompdf package is a wrapper package of PHP Dompdf package. With that, we can easily convert our view to PDF. In this post, I'll show you a step by step process on how to make PDF in Laravel application using the barryvdh/laravel-dompdf package.
Here, I'll show you the process of making a PDF for a customer list. According to this strategy, you can make your won custom dynamic PDF by your laravel application.
Note: This post is not a version-specific. Following this tutorial post, you can generate PDF by Laravel 5/6/7.
Step 01: Install the barryvdh/laravel-dompdf package.
composer require barryvdh/laravel-dompdf
Add these in config/app.php file.
add in the providers array
Barryvdh\DomPDF\ServiceProvider::class,
add in the aliases array
'PDF' => Barryvdh\DomPDF\Facade::class,
Step 02: Make a customer model.
php artisan make:model Customer -m
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model {
public $fillable = ['name', 'email', 'phone', 'dob'];
}
Step 03: Migration Create and run the migration.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCustomersTable extends Migration
{
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email');
$table->string('phone');
$table->string('dob');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('customers');
}
}
Now run the migration by php artisan migrate command.
Step 04: Define a route.
Route::get('admin/customers','CustomerController@index');
Step 05: Make a customer controller.
php artisan make:controller CustomerController
Now do the code for showing report and generating the PDF of that report.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Customer;
use PDF;
class CustomerController extends Controller
{
public function index(Request $request)
{
$data = Customer::all();
if ($request->has('export')) {
if ($request->get('export') == 'pdf') {
$pdf = PDF::loadView('customers.index-pdf', compact('data'));
return $pdf->download('customer-list.pdf');
}
}
return view('customers.index', compact('data'));
}
}
Here, we'll show the custom report with customers/index.blade.php view and export pdf with the customers/index-pdf.blade.php view when the user clicks on export pdf button from report view.
Step 6: Make the report and PDF view.
Report view - customers/index.blade.php
@extends('admin.template')
@section('content')
<div class="container mt-5">
<div class="d-flex justify-content-between mb-2">
<p><strong>Customer List</strong></p>
<a class="btn btn-primary" href="{{ url('admin/customers?export=pdf') }}">Export to PDF</a>
</div>
<table class="table table-bordered mb-5">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">E-mail</th>
<th scope="col">Phone</th>
<th scope="col">DOB</th>
</tr>
</thead>
<tbody>
@foreach($data as $row)
<tr>
<td>{{ $row->name }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->phone }}</td>
<td>{{ $row->dob }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection
PDF view - customers/index-pdf.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel PDF</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<h2 class="mb-3">Customer List</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>E-mail</th>
<th>Phone</th>
<th>DOB</th>
</tr>
</thead>
<tbody>
@foreach ($data as $row)
<tr>
<td>{{ $row->name }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->phone }}</td>
<td>{{ $row->dob }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
Hopefully, this step by step tutorial post will help you to learn how to generate dynamic PDF in the Laravel framework. If you find this post helpful then please share it with others.