Laravel 5 export to pdf using maatwebsite example

Laravel 5 export to pdf using maatwebsite example

In this post we will give you information about Laravel 5 export to pdf using maatwebsite example. Hear we will give you detail about Laravel 5 export to pdf using maatwebsite exampleAnd how to use it also give you demo for it if it is necessary.

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' => [

....

'MaatwebsiteExcelExcelServiceProvider',

],

'aliases' => [

....

'Excel' => 'MaatwebsiteExcelFacadesExcel',

],

Config

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
Also see:Laravel 5 import export to excel and csv using maatwebsite example.

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

use IlluminateDatabaseMigrationsMigration;

class CreateItemsTable extends Migration

{

public function up()

{

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

$table->increments('id');

$table->string('title');

$table->text('description');

$table->timestamps();

});

}

public function down()

{

Schema::drop("items");

}

}

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:

app/Item.php

namespace App;

use IlluminateDatabaseEloquentModel;

class Item extends Model

{

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

}


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

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:

app/Http/Controllers/MaatwebsiteDemoController.php

Also see:Laravel 5.7 – Generate PDF from HTML Example

use Input;

use AppItem;

use Excel;

class MaatwebsiteDemoController extends Controller

{

public function exportPDF()

{

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

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

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

{

$sheet->fromArray($data);

});

})->download("pdf");

}

}

Ok, Now we are ready to use our route, so let’s check…

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

Hope this code and post will helped you for implement Laravel 5 export to pdf using maatwebsite example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

78  +    =  87

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US