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' => [
     Barryvdh\DomPDF\ServiceProvider::class,
],

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

Step 3: Add Route

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

Route::get('pdfview',array('as'=>'pdfview','uses'=>'BestInterviewQuestionController@pdfview'));

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();
          view()->share('items',$items);
          if($request->has('download')){
             $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>
<table>
<tr>
<th>No</th>
<th>Title</th>
<th>Description</th>
</tr>
@foreach ($items as $key => $item)
   <tr>
     <td>{{ ++$key }}</td>
     <td>{{ $item->title }}</td>
     <td>{{ $item->description }}</td>
   </tr>
@endforeach
</table>