Laravel Export to PDF using Maatwebsite Example

By Hardik Savani November 5, 2023 Category : Laravel

In this post i will show you how to export or download pdf file from database table by maatwebsite. maatwebsite packages throught you can easily generate pdf file. so now i show you simple example of items table data, you can donwload in pdf formate. In following few step you can implement export both function in your project. we have to also need to use dompdf package for pdf generater. let's follow step.

Step 1: Installation

Open your composer.json file and add bellow line in required package.

Laravel 5

"maatwebsite/excel": "~2.1.0"

Laravel 4

"maatwebsite/excel": "~1.3"

Then, run command composer update

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

'providers' => [




'aliases' => [


'Excel' => 'Maatwebsite\Excel\Facades\Excel',



If you are using Laravel 5 then fire following command:

php artisan vendor:publish

If you are using Laravel 4 then fire following command:

php artisan config:publish maatwebsite/excel

This command will create config file for excel package.

At Last we have to also add dompdf package for pdf gererate so fire following command:

dompdf package for pdf

composer require dompdf/dompdf

Step 3: Create Table and Model

In this step we have to create migration for items table using Laravel 5 php artisan command, so first fire bellow command:

php artisan make:migration create_items_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateItemsTable extends Migration


public function up()


Schema::create('items', function (Blueprint $table) {







public function down()





After craete "items" table you should craete Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model


public $fillable = ['title','description'];


Step 3: Create Route and Controller

In this is step we need to create route of export file. so open your app/Http/routes.php file and add following route.

Route::get('exportPDF', 'MaatwebsiteDemoController@exportPDF');

Ok, now we should create new controller as MaatwebsiteDemoController in this path app/Http/Controllers/MaatwebsiteDemoController.php. this controller will manage data and genarate pdf file, so put bellow content in controller file:


use Input;

use App\Item;

use Excel;

class MaatwebsiteDemoController extends Controller


public function exportPDF()


$data = Item::get()->toArray();

return Excel::create('itsolutionstuff_example', function($excel) use ($data) {

$excel->sheet('mySheet', function($sheet) use ($data)







Ok, Now we are ready to use our route, so let's check...

Laravel 5 import export to excel and csv using maatwebsite example.

Tags :