export data to pdf in laravel using domPDF package

120 0 0 0

Last Updated : 2021-10-23 10:23:04

this snippet will help you to get started with domPDF package for laravel to export your data in pdf format

1- install domPDF package with composer


composer require barryvdh/laravel-dompdf

2- after installing the package with composer go to config directory and open app.php file and paste the following code in config/app.php file into providers array


Barryvdh\DomPDF\ServiceProvider::class,

3-  and paste this code into aliases array


'PDF' => Barryvdh\DomPDF\Facade::class,

4- now , publish the package configration to be able to use it properly with this code


php artisan vendo:publish --provider="Barryvdh\DomPDF\ServiceProvider"

5- after this stage you should provide route to the export process in web.php file


Route::get("/exportPDF", ['uses'=>'CouponController@exportPDF', 'as'=>'admin.coupons.export.pdf']);

6- in controller add this use statement and function to use


use PDF;  // to use pdf 


// this is the function to use when exporting
public function exportPDF(){
$coupons = Coupon::orderby('id', 'desc')->get();
$pdf = PDF::loadView('default.admin.couponsList_pdf',compact('coupons'))->setPaper('a4', 'portrait'); // you can use portrait or landscape to set paper orientation
return $pdf->download('couponsList_pdf.pdf');
}

7- finally you should create a view file to use when exporting like this for example


<!DOCTYPE html>
<html>
<head>
<title>Coupons List</title>

<style type="text/css">
#contacts {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}

#contacts td, #contacts th {
border: 1px solid #ddd;
padding: 8px;
}

#contacts tr:nth-child(even){background-color: #f2f2f2;}

#contacts tr:hover {background-color: #ddd;}

#contacts th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #a1a7a7;
color: #111;
}
.main-container{
float: none;
position: relative;
background-color: #eee;
padding: 8px;
border: 1px solid #ddd;
margin: 0 auto;
}
.table-container{
width: 75%;
display: table;

}
.heading-item a{
text-align: right;
}
.heading {padding: 10px 0px;}
.heading p {font-size:0;}
.heading p span { width:50%; display:inline-block; }
.heading p span.align-right { text-align:right; }

span a { font-size:16px; }
</style>
</head>
<body>
<div class="main-container">
<div class="heading">
<p>
<span>
<a>coupons List</a>
</span>
</p>
</div>
<div class="table-container">
<table id="contacts">
<tr>

<th>No</th>
<th>Code</th>
<th>Discount</th>

</tr>

@foreach ($coupons as $key => $coupon)

<tr>

<td>{{ $key+1 }}</td>

<td>{{ $coupon->code }}</td>

<td>{{ $coupon->discount }}</td>

</tr>

@endforeach
</table>
</div>
</div>
</body>
</html>

 


 

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.