Export pdf in laravel

Friends, many times we have to add such features to our website that we have to rate the invoice of the customer and give it to him or we have to give many other types of PDFs like ebook or any kind of bill.

For example, if a customer purchases our product in e-commerce, then we have to generate an invoice for him which is in PDF format. To create a similar feature in Laravel, we need to install a package. The name of this package is domPDF.

I will explain you an example how to use Dum PDF in Laravel:

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)->setPaper('a4', 'landscape');
return $pdf->download('all_member_lists.pdf'); 

} } 

php artisan optimize
view

@foreach($users as $user)
    {{$user->name}}
@endforeach

 

Leave a Reply