Let’s look at Laravel Datatables Export to PDF File Example. You’ll discover how to include an export PDF button in a Yajra datatable. This article discusses the laravel yajra datatable pdf export button in detail. Laravel yajra datatable export buttons explained step by step in pdf.
Creating a basic example of datatables pdf with button in Laravel 6, 7, 8, and 9 is shown here.In this post, I’ll show you how to add pdf buttons to Laravel Yajra Datatables step by step. To add an export button to your datatables, we will use the yajra/laravel-datatables-buttons package. You can easily export table data into a pdf file, which will assist you in exporting it.Snappy must be used to generate a PDF file from a Laravel Yajra datatable.
Step 1: Install Laravel
composer create-project --prefer-dist laravel/laravel blog
Step 2 : Install Yajra Datatable
We need to install the yajra datatable composer package for datatable and the yajra/laravel-datatables-buttons export buttons, which you can do with the command:
composer require yajra/laravel-datatables-oracle
composer require yajra/laravel-datatables-buttons
After that you need to set providers and alias.
config/app.php
.....
'providers' => [
....
Yajra\DataTables\DataTablesServiceProvider::class,
Yajra\DataTables\ButtonsServiceProvider::class,
]
'aliases' => [
....
'DataTables' => Yajra\DataTables\Facades\DataTables::class,
]
.....
Now you have to run bellow command to get configuration file, so let’s run bellow command:
php artisan vendor:publish --tag=datatables-buttons
Step 3: Install laravel-snappy
In this section, we will install the laravel-snappy composer package in order to export data to a PDF file. So, in this step, let’s do a few things.
Install the Laravel Snappy dependencies.
Note : If you have 32-bit system then install this dependencies
composer require h4cc/wkhtmltopdf-i386 0.12.x
composer require h4cc/wkhtmltoimage-i386 0.12.x
Note : If you have 64-bit system then install this dependencies
composer require h4cc/wkhtmltopdf-amd64 0.12.x
composer require h4cc/wkhtmltoimage-amd64 0.12.x
Now, run the following command to install the laravel-snappy composer package:
composer require barryvdh/laravel-snappy
After that you need to set providers and alias.
config/app.php
.....
'providers' => [
....
Barryvdh\Snappy\ServiceProvider::class,
]
'aliases' => [
....
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
]
.....
Now, in order to obtain the configuration file, execute the following command:
php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
Now follow this things:
Note : After copy both folder then give 777 permission
cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/
cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin/
Step 4: Add Dummy Records
In this step, we will use Tinker Factory to create some dummy users. So, let’s make some dummy records with the following command:
php artisan tinker
factory(App\User::class, 200)->create();
Step 5: Create DataTable Class
Using the Yajra Datatable command, we must create the User DataTable class. So, let’s run the following command:
php artisan datatables:make Users
You’ve made a new DataTable class file. So, let’s update the file below. Here we will add a display column, buttons, and other elements.
app/DataTables/UsersDataTable.php
<?php
namespace App\DataTables;
use App\User;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;
class UsersDataTable extends DataTable
{
/**
* Build DataTable class.
*
* @param mixed $query Results from query() method.
* @return \Yajra\DataTables\DataTableAbstract
*/
public function dataTable($query)
{
return datatables()
->eloquent($query);
}
/**
* Get query source of dataTable.
*
* @param \App\User $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query(User $model)
{
return $model->newQuery();
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
return $this->builder()
->setTableId('users-table')
->columns($this->getColumns())
->minifiedAjax()
->orderBy(1)
->parameters([
'dom' => 'Bfrtip',
'buttons' => ['pdf'],
]);
}
/**
* Get columns.
*
* @return array
*/
protected function getColumns()
{
return [
Column::make('id'),
Column::make('name'),
Column::make('email'),
];
}
/**
* Get filename for export.
*
* @return string
*/
protected function filename()
{
return 'Users_' . date('YmdHis');
}
}
Step 6: Add Route
In this step, we’ll create a route for the datatables layout file and another for getting data. So, open your routes/web.php file and add the route below.
routes/web.php
Route::get('users', 'UserController@index');
Step 7: Create Controller
At this point, we should make a new controller called UserController. This controller will handle layout, data requests, and response, so include the following content in the controller file:
app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\DataTables\UsersDataTable;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(UsersDataTable $dataTable)
{
return $dataTable->render('users');
}
}
Step 8: Create View
Let’s make a layout file called users.blade.php (resources/views/users.blade.php) and add the following code:
resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel Yajra Datatables Export to Excel Button Example - ItSolutionStuff.com</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.0.3/js/dataTables.buttons.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.0.3/css/buttons.dataTables.min.css">
<script src="/vendor/datatables/buttons.server-side.js"></script>
</head>
<body>
<div class="container">
<h1>Laravel Yajra Datatables Export to Excel Button Example - ItSolutionStuff.com</h1>
{!! $dataTable->table() !!}
</div>
</body>
{!! $dataTable->scripts() !!}
</html>
Now that we’re ready to run our example, type the following command for a quick run:
php artisan serve
You can now open the following URL in your browser:
http://localhost:8000/users
Happy coding nerding out.