Add Header & Footer in DOMPDF

Add Header & Footer in DOMPDF

Install dompdf package

composer require barryvdh/laravel-dompdf
Enable GD & zip extension first (php.ini), then use this cmd.
Goto config / app.php

'providers' => [
  Barryvdh\DomPDF\ServiceProvider::class,
];

'aliases' => [
  'PDF' => Barryvdh\DomPDF\Facade::class,
];
php artisan make:controller PDFController

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller
{

function member_list_pdf(Request $req){
$data = [
  'users' => $users, 'age_from' => $age_from, 'age_to' => $age_to, 'member_code' => $member_code, 'matital_status' => $matital_status, 'religion_id' => $religion_id, 'caste_id' => $caste_id, 'sub_caste_id' => $sub_caste_id, 'mother_tongue' => $mother_tongue, 'profession' => $profession, 'country_id' => $country_id, 'state_id' => $state_id, 'city_id' => $city_id, 'min_height' => $min_height, 'max_height' => $max_height, 'member_type' => $member_type
];

$pdf = PDF::loadView('frontend.member.member_listing.print_pdf', $data);
return $pdf->download('all_member_lists.pdf');
}

}

php artisan optimize
<div class="container-fluid">

<?php 

$username = $diets[0]->user_name;
$user_age = $diets[0]->user_age;
$user_diet_preference = $diets[0]->user_diet_preference;

$diet_date_to = $_GET['diet_date_to'];
$diet_date_from = $_GET['diet_date_from'];

?>

<div class="row">
<div class="col-lg-12">
<div class="card" style="background-image:url({{asset('public')}}/pran.png); background-repeat:no-repeat; background-position:center center">
<div class="card-body">

<style>
@page { margin: 100px 25px; }
header { position: fixed; top: -60px; left: 0px; right: 0px; background-color: lightblue; height: 50px; }
footer { position: fixed; bottom: -60px; left: 0px; right: 0px; background-color: lightblue; height: 50px; }
p { page-break-after: always; }
p:last-child { page-break-after: never; }
</style> 

<?php
   $meals1 = DB::table('meals')->where('user_id', $userid)->where('meal', '!=', '')->whereDate('diet_date', '>=', $diet_date_to)->whereDate('diet_date', '<=', $diet_date_from)->orderBy('diet_date','DESC')->get()->unique('diet_date');
?>

@foreach($meals1 as $meal1)

<!-- header start -->

<header> 
<div style="position:fixed; top:0" id="header"> 
<div class="invoice-title">

<div class="mb-4">
<h4 class="mb-1 text-muted" style="margin-left:25px">pranshakti.in</h4>
<img src="{{asset('public')}}/pran_bg.png" class="logo" width="150" />
</div>

<div style="position:fixed; right:0; margin-top:-193px">
<center>
<h4 class="">{{$username}}</h4>
<h5 class="">Age: {{$user_age}}</h5>
<h5 class="">Diet Preference: {{$user_diet_preference}}</h5>
<h6 style="color:grey; margin-top:10px">
<?php

echo $diet_date_to .' To '.$diet_date_from;

?>
</h6>
</center>
</div>

</div>
</div> 
</header> 
<!-- header end -->


<!-- footer start -->

<footer>
<div class="footer" id="footer" style="background:#f4f4f4; padding-top:10px; position:fixed; bottom:0; width:100%">
<center><p>Copyright © Pranshakti {{date('Y')}}</p></center>
</div>
</footer>

<!-- content end -->

<main>

<div class="row" style="position:relative; margin-top:180px" id="content">

<div style="background:#f7f7f7; padding:10px">
<h4>{{$meal1->diet_date}}</h4>
</div>
<?php
$meals2 = DB::table('meals')->where('user_id', $userid)->whereDate('diet_date', $meal1->diet_date)->orderBy('diet_date','DESC')->get();
?>
<table class="table table-striped">
@foreach($meals2 as $meal2)

<tr>
<td><span style="font-weight:bold">Diet Time</span> : {{$meal2->diet_time}}</td> &nbsp; &nbsp; 
<td> <span style="font-weight:bold">Meal</span> : {{$meal2->meal}}</td> &nbsp; &nbsp; 
</tr>
<hr/>
@endforeach
</table>

</div>
</main>
@endforeach

<!-- end row -->
<!--</div>-->

</div>
</div><!-- end col -->

</div>
</div>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

Leave a Reply