Thứ Hai, 19 tháng 12, 2016

Laravel 5 Tutorial : How to Export to PDF using Dompdf Library with Example


Laravel 5.3 Tutorial For Beginners : This lessons will show you how to Export to PDF using Dompdf Library with Example in laravel 5.3. At the previews lessons we have learn how to create multiple upload form in laravel 5.3, so please read :

Laravel 5.3 Multiple File Upload & Save Into Database

Video Tutorial How to Export to PDF using Dompdf Library with Example



Full source code

Controller (PDFController.php)

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
use App\datatoPDF;
class PDFController extends Controller
{
    // show all data
    public function index(Request $req)
    {
      // show all data to index
      $blogs = datatoPDF::all();
      view()->share('blogs',$blogs);
      if($req->has('download')){
        $pdf = PDF::loadView('pdf')->setPaper('a4', 'landscape');
        return $pdf->download('pdf');
      }
      return view('index');
    }
}

Model (datatoPDF.php)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class datatoPDF extends Model
{
    protected $table = 'users';
}

View (index.blade.php)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>All Users Data</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

    <div class="container">
      <h2>All Users Data</h2>
      <div class="btn-group">
        <button class="btn btn-primary btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Export All Data <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
          <li><a href="{{ route('htmltopdf',['download'=>'pdf']) }}">Export to PDF</a></li>
        </ul>
      </div>
      <table class="table table-striped">
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Password</th>
            <th>Created At</th>
            <th><button class="btn btn-success btn-xs">Add New Supplier</button></th>
          </tr>
        </thead>
        <tbody>
          @foreach($blogs as $blog)
            <tr>
              <td>{{ $blog->id }}</td>
              <td>{{ $blog->name }}</td>
              <td>{{ $blog->email }}</td>
              <td>{{ $blog->password }}</td>
              <td>{{ $blog->created_at }}</td>
              <td>
                <button class="btn btn-warning btn-xs btn-detail">
                  <span class="glyphicon glyphicon-edit"></span>
                </button>
                <button class="btn btn-danger btn-xs btn-delete">
                  <span class="glyphicon glyphicon-trash"></span>
                </button>
              </td>
            </tr>
          @endforeach
        </tbody>
      </table>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </body>
</html>

pdf.blade.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>All User data To PDF</title>
  </head>
  <body>
    <div class="container">
      <h2>All Users Data PDF</h2>
      <table class="table table-striped">
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Password</th>
            <th>Created At</th>
          </tr>
        </thead>
        <tbody>
          @foreach($blogs as $blog)
            <tr>
              <td>{{ $blog->id }}</td>
              <td>{{ $blog->name }}</td>
              <td>{{ $blog->email }}</td>
              <td>{{ $blog->password }}</td>
              <td>{{ $blog->created_at }}</td>
            </tr>
          @endforeach
        </tbody>
      </table>
    </div>
  </body>
</html>

Download Full Source Code here.

See you next lessons ...

1 nhận xét: