How to Set Header and Footer in Laravel PDF?
Teams applying “How to Set Header and Footer in Laravel PDF?” in a production project can use standard deviation to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi Dev,
Today, in this article I will share with you something new about how to make your own custom header and footer dompdf in laravel application, we will show an example of header and footer dompdf in laravel.
One of the prevalent issues developers have with this library is setting the header and footer of pages for the PDF file, and that is what this article is about.
Here, I will give you a full example of how to add header and footer in pdf using laravel in laravel as below so follow my all steps.
Step 1 : Install Laravel
In the first step, we need to get fresh laravel version application So let's open terminal and run bellow command to install fresh laravel project.
composer create-project --prefer-dist laravel/laravel blog
Step 2 : Install dompdf Package first of all we will install barryvdh/laravel-dompdf composer package by following composer command in your laravel application.
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,
]
Step 3: Create Route
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 4:Create Controller
In this step,we will create a controller. Use the below command for generate controller
Path : app/Http/Controllers/PDFController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class PDFController extends Controller
{
/**
* Write Your Code..
*
* @return string
*/
public function generatePDF()
{
$data = [
'title' => 'Welcome to Nicesnippets.com',
'date' => date('m/d/Y')
];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('nicesnippets.pdf');
}
}
Step 5:Create a blade view
In this step, we will create a blade file name myPDF.blade.php bellow following path.
Path : resources/views/myPDF.blade.php
<html>
<head>
<style>
/** Define the margins of your page **/
@page {
margin: 100px 25px;
}
header {
position: fixed;
top: -60px;
left: 0px;
right: 0px;
height: 50px;
font-size: 20px !important;
/** Extra personal styles **/
background-color: #008B8B;
color: white;
text-align: center;
line-height: 35px;
}
footer {
position: fixed;
bottom: -60px;
left: 0px;
right: 0px;
height: 50px;
font-size: 20px !important;
/** Extra personal styles **/
background-color: #008B8B;
color: white;
text-align: center;
line-height: 35px;
}
</style>
</head>
<body>
<!-- Define header and footer blocks before your content -->
<header>
Nicesnippets.com
</header>
<footer>
Copyright © <?php echo date("Y");?>
</footer>
<!-- Wrap the content of your PDF inside a main tag -->
<main>
<p style="page-break-after: always;">
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>
<p style="page-break-after: never;">
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>
</main>
</body>
</html>
Preview
Now, we will use the php artisan serve command.
php artisan serve
Now we are ready to run our example so run bellow command to quick run.
http://localhost:8000/generate-pdf
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial