Loading Posts...

Laravel 6 Generate PDF From View Example Tutorial From Scratch

Laravel 6 Generate PDF From View Example Tutorial From Scratch

Laravel 6 Generate PDF From View Example Tutorial From Scratch is today’s topic. If you want to know a basic laravel functionality, then check out my article Laravel 6 Crud Example From ScratchFor this example, we use the Dompdf library. Dompdf is an HTML to PDF converter The dompdf is (mostly) the CSS 2.1 compliant HTML layout and rendering engine written in PHP language.

It is the style-driven renderer: it will download and read an external stylesheets, inline style tags, and all the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

Laravel 6 Generate PDF From View

If you are using an older version of Laravel, then check out this How To Generate PDF In Laravel 5 tutorial.

Step: 1 Download the laravel-dompdf package

Type the following command to install the laravel-dompdf package.

composer require barryvdh/laravel-dompdf
➜  laravel6 git:(master) composer require barryvdh/laravel-dompdf
Using version ^0.8.5 for barryvdh/laravel-dompdf
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing sabberworm/php-css-parser (8.3.0): Downloading (100%)
  - Installing phenx/php-svg-lib (v0.3.3): Downloading (100%)
  - Installing phenx/php-font-lib (0.5.1): Downloading (100%)
  - Installing dompdf/dompdf (v0.8.3): Downloading (100%)
  - Installing barryvdh/laravel-dompdf (v0.8.5): Downloading (100%)
dompdf/dompdf suggests installing ext-imagick (Improves image processing performance)
dompdf/dompdf suggests installing ext-gmagick (Improves image processing performance)
Writing lock file
Generating optimized autoload files
> IlluminateFoundationComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: barryvdh/laravel-dompdf
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.

Step: 2 Configure the package laravel 6 application

So go to a config >> app.php and add the following configuration.

'providers' => [
'aliases' => [
    'PDF' => BarryvdhDomPDFFacade::class,

Here we are registering a PDF register provider for our application and also set the alias for it. So when we need to create PDF, we need to include it in our namespace like,

use PDF;

Then, we instantiate a PDF class and use its API for further enhancing the generated PDF file.

Step: 3 Create a layout blade file

Inside the resources >> views folder, create a new file called layout.blade.php.

  Laravel 6 CRUD Example


Step 4: Create model and migration files

We will create model and migration files using the following command.

php artisan make:model Disneyplus -m

Now, go to the [timestamp].create_disneypluses_table.php file and add the columns.

public function up()
        Schema::create('disneypluses', function (Blueprint $table) {

Now, migrate the database using the following command.

php artisan migrate

Step 5: Create a controller and routes

Next step is to create a DisneyplusController.php file.

php artisan make:controller DisneyplusController

Now, add the two routes inside the routes >> web.php file.

// web.php

Route::get('disneyplus', '[email protected]')->name('disneyplus.create');
Route::post('disneyplus', '[email protected]')->name('disneyplus.store');

Now, create two methods inside the DisneyplusController.php file. 

Remember to import the PDF module by use PDF; code.

Step: 6 Create a form blade file for input the data

Now, inside the views folder, create one file called form.blade.php file. Add the following code.



Add Disneyplus Shows
@if ($errors->any())
    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach


Step 7: Store data in the database

Now, we will write the two functions inside the DisneyplusController.php file.

            'show_name' => 'required|max:255',
            'series' => 'required|max:255',
            'lead_actor' => 'required|max:255',
        return redirect('/disneyplus')->with('success', 'Disney Plus Show is successfully saved');

So, in the above file, first, we have shown the form file, and then inside the store function, we check for validation and then store the data into the database.

Also, add the fillable fields inside the Disneyplus.php model file.

Now, go to this route: http://laravel6.test/disneyplus or http://localhost:8000/disneyplus

You will see one form. Try to save the data, and if everything in the code is right, then, you will see one entry in the database.

Step: 8 Create a view file for display the data.

Before we create a view file, we need to add one route inside the web.php.

// web.php

Route::get('disneyplus/list', '[email protected]')->name('disneyplus.index');

Now, create a view file called list.blade.php file. Add the following code.


    @foreach($shows as $show)
ID Show Name Series Lead Actor Action
{{$show->id}} {{$show->show_name}} {{$show->series}} {{$show->lead_actor}}

Now, add the code inside the index() function of DisneyplusController.php file.

public function index()
        $shows = Disneyplus::all();

        return view('list', compact('shows'));

Now, go to the http://laravel6.test/disneyplus/list or http://localhost:8000/disneyplus/list

You will see the listing of the shows.

Step: 9 Create a route to download the pdf file

Add the following code inside the route file.

// web.php

Route::get('/downloadPDF/{id}','[email protected]');

Now, update the list.blade.php file and add the Download PDF link.


    @foreach($shows as $show)
ID Show Name Series Lead Actor Action
{{$show->id}} {{$show->show_name}} {{$show->series}} {{$show->lead_actor}} Download PDF

Step: 10 Create pdf.blade.php file to design our pdf

You can create the new DOMPDF instance and load an HTML string, file, or blade view name. You can save it to the file, or stream.

Okay, inside the views folder, create one file called pdf.blade.php file and add the following code.

Show Name Series Lead Actor
{{$show->show_name}} {{$show->series}} {{$show->lead_actor}}

We have created a simple table which will be generated inside the PDF.

Step: 11 Write a controller function to download the PDF

Write the following code inside the DisneyplusController.php file.

// DisneyplusController.php

public function downloadPDF($id) {
        $show = Disneyplus::find($id);
        $pdf = PDF::loadView('pdf', compact('show'));
        return $pdf->download('disney.pdf');

Now, go to the http://laravel6.test/disneyplus/list or http://localhost:8000/disneyplus/list and click on the Download PDF link.

You will see that it downloads the PDF file and open that PDF, you will see the table contains show listings.


This was the simple example of Laravel 6 Generate PDF. Finally, our tutorial on How to Generate PDF in Laravel 6 is over.

The post Laravel 6 Generate PDF From View Example Tutorial From Scratch appeared first on AppDividend.

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.


The author didnt add any Information to his profile yet

Loading Posts...