Laravel DomPDF Table with Page Break Example

By Hardik Savani November 5, 2023 Category : Laravel

Hi Guys,

I will explain step by step tutorial laravel dompdf table page break. I explained simply about dompdf table multiple pages. We will use dompdf table example. let’s discuss about how to table break in dompdf laravel. follow the below step for barryvdh/laravel-dompdf table page break.

At times, we encounter PDF files containing multiple pages of tabular data. It's essential to correctly handle page breaks to avoid problems with table headers. I will demonstrate how to manage tables with page breaks using the chunk() method and the page-break-after: always CSS property. Let's proceed with the following steps.

Now, let's see an example step by step, you can use this example with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 versions:

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install DomPDF Package

next, we will install the DomPDF package using the following composer command, let's run the below command:

composer require barryvdh/laravel-dompdf

Step 3: Create Controller

In this step, we will create PDFController with generatePDF() where we write the code of generate pdf. so let's create controller using bellow command.

php artisan make:controller PDFController

Now, update the code on the controller file.

app/Http/Controllers/PDFController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;

use App\Models\User;

class PDFController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function generatePDF()

{

$users = User::all();

$users = $users->chunk(30);

$data = [

'title' => 'Welcome to ItSolutionStuff.com',

'users' => $users

];

$pdf = PDF::loadView('myPDF', $data);

return $pdf->download('itsolutionstuff.pdf');

}

}

Step 4: Add Route

Furthermore, open routes/web.php file and update code on it.

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\PDFController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('generate-pdf', [PDFController::class, 'generatePDF']);

Step 5: Create View File

In Last step, let's create myPDF.blade.php and myPDFTable.blade.php for layout of pdf file and put following code:

resources/views/myPDF.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 10 Generate PDF Example - ItSolutionStuff.com</title>

<style>

.page-break {

page-break-after: always;

}

table.table-bordered > thead > tr > th{

border:1px solid #a1a1a1;

padding: 4px;

}

table.table-bordered > tbody > tr > td{

border:1px solid #a1a1a1;

padding: 4px;

}

.table{

width: 100%;

}

</style>

</head>

<body>

<div>

@foreach($users as $data)

@include('myPDFTable', ['data' => $data])

@endforeach

</div>

</body>

</html>

resources/views/myPDFTable.blade.php

<table class="table table-bordered {{ !$loop->last ? 'page-break' : ''}}">

<thead>

<tr>

<th>ID</th>

<th>Name</th>

<th>Email</th>

</tr>

</thead>

<tbody>

@foreach($data as $user)

<tr>

<td>{{ $user->id }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>

</tr>

@endforeach

</tbody>

</table>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/generate-pdf

you will download the file below:

Now we are ready to run this example and check it...

I hope it can help you...

Tags :