Generate html view file to PDF and support download


Usually, if we are working on a large ERP project level in laravel, we need to create a PDF file for data from database tables. In this tutorial, I give you a very simple way to create a pdf file and provide step by step downloading that pdf for beginners.

In this post I use the laravel-dompdf package to create a pdf file and this package also provides a download function. In this example I have "items" table and I will download pdf file of item table data with tabular format, also you can write your own css on view file for pdf, so you can create better pdf file.

So finally you just need to follow few steps and get pdf download and result, first you have a new project Laravel with work.


In first step we have to download laravel-dompdf plugin to generate pdf file from blade view file. So first run bellows command in your terminal:

composer require barryvdh/laravel-dompdf

Now open the config/app.php file and add the service provider and aliases.

'providers' => [



'aliases' => [


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


Add Route

In this step we need to add a route to generate the view. so go to your  app/Http/routes.php  file and add the following routes.


Create Controller

Ok, now we have to create a new controller as ItemController in this path  app/Http/Controllers/ItemController.php . Make sure you must have item table with some data. this controller will manage data and generate pdf file, so put below content in controller file:


namespace App\Http\Controllers;

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

class ItemController extends Controller

     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function pdfview(Request $request)
        $items = DB::table("items")->get();

            $pdf = PDF::loadView('pdfview');
            return $pdf->download('pdfview.pdf');

        return view('pdfview');

Create View File

In last step, we have to create view file "pdfview.blade.php" to generate view file as well as pdf, so create pdfview file and put bellows code:


<style type="text/css">
    table td, table th{
        border:1px solid black;
<div class="container">

    <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>

Now you can run and check,


Jasa pembuatan Aplikasi Mobile dan Web. Siap memberikan solusi digital untuk bisnis anda. Tugas Kuliah, Organisasi, Perusahaan, E-Commerce.