Laravel 5.6 PDF Generation using Dompdf Example

Today, We want to share with you Laravel 5.6 PDF Generation using Dompdf Example.In this post we will show you PHP Laravel 5.6 PDF Generation using FPDF, hear for Best open source PDF generation libraries for Laravel 5.6 we will give you demo and example for implement.In this post, we will learn about Laravel 5.6 generate PDF from html view file and download using dompdf with an example.

Laravel 5.6 PDF Generation using Dompdf Example

There are the Following The simple About Laravel 5.6 PDF Generation using Dompdf Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop , so the some major files and Directory structures for this example is following below.

Laravel 5.6 PDF Generate Tutorial

  • Setup Laravel Project
  • Settings SQL Database
  • Setup Latest package in laravel-dompdf
  • Include Model and Migration File
  • Make a View File
  • Make one controller
  • Laravel 5.6 Route
  • Save All data to the database
  • Laravel 5.6 show the data.
  • Simple design Laravel 5.6 pdf blade file
  • Laravel 5.6 controller to simple download the PDF

Laravel 5.6 PDF Generation using Dompdf Tutorial With Example

I am Gonna to Configure step by step laravel 5.6 tutorial pdf Project.

Step #1: Setup Laravel 5.6 Project

composer create-project --prefer-dist laravel/laravel pdfgenerate

Sytep #2: Settings SQL Database

setup database credentials >> .env file

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=topdevlopers
DB_USERNAME=atmiya25
[email protected]

Step #3: Setup laravel-dompdf package

install The laravel-dompdf – laravel 5.6 tutorial pdf

composer require barryvdh/laravel-dompdf

Include Laravel 5.6 ServiceProviders in config/app.php

//laravel 5.6 tutorial pdf providers
'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],

Include Laravel 5.6 config/app.php

//some laravel 5.6 tutorial pdf aliases
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
],

Step #4: Setup Laravel Model and Migration File

php artisan make:model TopDevloperInfo -m

Laravel Project Save as well as run.

php artisan migrate

Step #5: Make a Html View File

resources/views/topdevloperinfo.blade.php

<!-- topdevloperinfo.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel 5.6 PDF Generation using Dompdf Example</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">  
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>    </head>
  <body>
    <div class="container">
      <h2>Laravel 5.6 PDF Generation using Dompdf Example</h2><br/>
	  <p>laravel 5.6 tutorial pdf </p>
      <form method="post" action="{{url('/topdevloperinfo/store')}}">
        @csrf
        <div class="form-group row">
            <label for="name" class="col-md-4 col-form-label text-md-right">Name:</label>
            <div class="col-md-2">
                <input id="name" type="text" class="form-control" name="name">
            </div>
        </div>
        <div class="form-group row">
            <label for="language" class="col-md-4 col-form-label text-md-right">Language:</label>
            <div class="col-md-2">
                <input id="language" type="text" class="form-control" name="language">
            </div>
        </div>
        <div class="form-group row">
            <label for="position" class="col-md-4 col-form-label pakainfo text-md-right">Position:</label>
            <div class="col-md-2">
                <input id="position" type="text" class="form-control" name="position">
            </div>
        </div>
        <div class="form-group row">
            <label for="devpoint" class="col-md-4 col-form-label text-md-right">Dev Point:</label>
            <div class="col-md-2">
                <input id="devpoint" type="text" class="form-control" name="devpoint">
            </div>
        </div>
      <div class=" form-group row">
          <div class="col-md-4"></div>
          <div class="col-md-2">
            <button type="submit" class="btn btn-success">Submit</button>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>

Step #6: Make Laravel 5.6 controller

php artisan make:controller  TopDevloperInfoController

Create TopDevloperInfoController.php

//TopDevloperInfoController.php

public function create()
    {
        return view('topdevloperinfo');
    }

Step #7: Laravel 5.6 Route

Laravel 5.6 web.php file

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

Step #8: Save data to the Mysql database

TopDevloperInfoController.php

//TopDevloperInfoController.php

use App\TopDevloperInfo;

public function store(Request $request)
    {
        $devloper = new TopDevloperInfo();
        $devloper->name = $request->get('name');
        $devloper->language = $request->get('language');
        $devloper->position = $request->get('position');
        $devloper->devpoint = $request->get('devpoint');
        $devloper->save();
        return redirect('/topdevloperinfo/index');
    }

TopDevloperInfoController.php

//TopDevloperInfoController.php

public function index()
    {
        $devlopers = TopDevloperInfo::all();
        return view('index', compact('devlopers'));
      }

Step # 9: Make a HTML file for the Display the All data.

resources/views/index.blade.php

<!-- Laravel 5.6 index.blade.php -->
<!DOCTYPE html>
<html>
  <head>
  <title>laravel 5.6 tutorial pdf</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
  </head>
  <body>
      <div class="container">
      <table class="table table-striped">
  <thead>
    <th>ID</th>
    <th>Name</th>
    <th>Language</th>
    <th>Position</th>
    <th>Dev Point</th>
    <th>Action</th>
  </thead>
  <tbody>
    @foreach($devlopers as $devloper)
    <tr>
      <td>{{$devloper->id}}</td>
      <td>{{$devloper->name}}</td>
      <td>{{$devloper->language}}</td>
      <td>{{$devloper->position}}</td>
      <td>{{$devloper->devpoint}}</td>
      <td><a href="{{action('[email protected]', $devloper->id)}}">PDF</a></td>
    </tr>
    @endforeach
  </tbody>
</table>
      </div>
  </body>
</html>

Step #10: Make a HTML file to design Simple pdf blade

resources/views/pdf.blade.php

<!-- pdf.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Generate PDF from HTML in PHP Laravel 5.6 using Dompdf Library</title>
  </head>
  <body>
    <table class="table table-bordered">
      <tr>
        <td>
          {{$devloper->name}}
        </td>
        <td>
          {{$devloper->language}}
        </td>
        <td>
          {{$devloper->position}}
        </td>
        <td>
          {{$devloper->devpoint}}
        </td>
      </tr>
    </table>
  </body>
</html>

Step #11: Laravel 5.6 function in the controller to PDF download

TopDevloperInfoController.php

//TopDevloperInfoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\TopDevloperInfo;
use PDF;

class TopDevloperInfoController extends Controller
{
    public function create()
    {
        return view('topdevloperinfo');
    }

    public function store(Request $request)
    {
        $devloper = new TopDevloperInfo();
        $devloper->name = $request->get('name');
        $devloper->language = $request->get('language');
        $devloper->position = $request->get('position');
        $devloper->devpoint = $request->get('devpoint');
        $devloper->save();
        return redirect('/topdevloperinfo/index');
    }

    public function index()
    {
        $devlopers = TopDevloperInfo::all();
        return view('index', compact('devlopers'));
      }

      public function downloadPDF($id)
      {
        $devloper = TopDevloperInfo::find($id);
        $pdf = PDF::loadView('pdf', compact('devloper'));
        return $pdf->download('topdevloperinfo.pdf');
      }
}

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about create Simple Generate PDF file in Laravel 5.6.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Calculate days between two dates in jquery
  2.   Remove specific value from php array
  3.   How to export mysql data to pdf using php with demo?
  4.   global variable for all controller and views in Laravel
  5.   Laravel 6 Count from Join Example
  6.   Get maximum key value of array in PHP
  7.   Laravel Handling findOrFail exceptions
  8.   Remove empty values from array in PHP
  9.   PHP Laravel 5 Class Form HTML not found Resolve Error
  10.   Filter - Limit the length of string using AngularJS