Site icon FahmidasClassroom

How to setup print option in Laravel

Laravel Print

Steps:
1. Select any table with data from the database that you want use for printing. Here students table is selected from the database.
2. Create a controller named PrintController

php artisan make:controller PrintController

3. Create a Course model for courses table

php artisan make:model Student

You can download or use the jquery print plugin from the following location. I have downloaded it to use it offline.
http://www.position-absolute.com/creation/print/jquery.printPage.js


4. Create two view files named blade.php and printstudent.blade.php
5. Add the code for routing


Route::get('/students','PrintController@index');
Route::get('/prnpriview','PrintController@prnpriview');

6. Create a my.blade.php view file in layouts folder to link jquery and bootstap

<html>
<head>
<link rel="stylesheet" href="css/bootstrap.css">

<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="js/jquery.printPage.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-12">
@yield('content')
</div>
</div>
</body>
</html>

7. Add the following code in the controller

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Student;
class PrintController extends Controller
{
      public function index()
      {
            $students = Student::all();
            return view('printstudent')->with('students', $students);;
      }
      public function prnpriview()
      {
            $students = Student::all();
            return view('students')->with('students', $students);;
      }
}

8. Add the following code in students.blade.php file

@extends('layouts.my')
@section('content')
<center>
<h1>Student Information List </h1>
<table class="table" >
<tr><th>Id</th><th>Name</th><th>Email</th><th>Course</th></tr>
@foreach($students as $student)
<tr><td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->course }}</td>
</tr>
@endforeach
</center>
@endsection



9. Add the following code in printstudent.blade.php file

@extends('layouts.my')
@section('content')
<center>
<br><br>
<a href="{{ url('/prnpriview') }}" class="btnprn btn">Print Preview</a></center>
<script type="text/javascript">
$(document).ready(function(){
$('.btnprn').printPage();
});
</script>
<center>
<h1>Course List </h1>
<table class="table" >
<tr><th>Id</th><th>Name</th><th>Email</th><th>Course</th></tr>
 @foreach($students as $student)
<tr><td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->email }}</td>
<td>{{ $student->course }}</td> </tr>
@endforeach
</center>
@endsection



10. Run from the browser

The steps are shown in the following tutorial.

Exit mobile version