How to generate pdf in laravel

We can generate PDF in laravel using following steps with laravel-dompdf package.

Step 1: Installation

composer require barryvdh/laravel-dompdf

Step 2: Add service provider

Now open config/app.php file and add the following code.

'providers' => [

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

Step 3: Add Route

Open routes/web.php file and add the following route.


Step 4: Create Controller

make a file in app/Http/Controllers/BestInterviewQuestionController.php and add the given code.

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

class ItemController extends Controller
    public function pdfview(Request $request)
          $items = DB::table("items")->get();
             $pdf = PDF::loadView('pdfview');
             return $pdf->download('pdfview.pdf');
          return view('pdfview');

Step 5: Create View File

Create a view file in resources/view/pdfview.blade.php and add the following code.

<a href="{{ route('pdfview',['download'=>'pdf']) }}">Download PDF</a>
@foreach ($items as $key => $item)
     <td>{{ ++$key }}</td>
     <td>{{ $item->title }}</td>
     <td>{{ $item->description }}</td>