How to create dompdf in laravel
We will discuss how to generate the Laravel 7 PDF file from the blade view or HTML view in Laravel 7 using laravel-dompdf package. when we need to generate the invoice, booking ticket generate, large data convert into pdf, and many more things.
Overview :
Step 1: Install Laravel
Step 2: Create Table
Step 3: Install laravel-dompdf Package
Step 4: Add providers and aliases
Step 5: Create Route
Step 6: Create Model and Controller
Step 7: Create Blade Files
Step 1: Install Laravel
Also Read : How to Install Laravel and Basic Configuration
Step 2: Create Table
We need to create a migration. so below command using create the jobsheets table migration.
php artisan make:migration create_jobsheets_table --create=jobsheets
After complete migration. we need below changes the database/migrations/create_jobsheets_table.php file.
#database/migrations/create_jobsheets_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateJobsheetsTable extends Migration
{
public function up()
{
Schema::create('jobsheets', function (Blueprint $table) {
$table->id();
$table->string('company_name')->nullable();
$table->string('dates')->nullable();
$table->string('job_quantity')->nullable();
$table->string('size')->nullable();
$table->longText('papers_and_colors')->nullable();
$table->longText('quantity_and_size')->nullable();
$table->longText('note')->nullable();
$table->string('job_name')->nullable();
$table->string('color_of_ink')->nullable();
$table->string('numbering')->nullable();
$table->string('rulling')->nullable();
$table->string('punching')->nullable();
$table->string('perphoration')->nullable();
$table->string('fixed_copy')->nullable();
$table->string('type_of_binding')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('jobsheets');
}
}
?>
Run the below command. to create jobsheets table.
php artisan migrate
Step 3: Install laravel-dompdf Package
Now, you install laravel-dompdf package using below command.
composer require barryvdh/laravel-dompdf
Step 4: Add providers and aliases
Add providers and aliases code in the config/app.php file.
#config/app.php
'providers' => [
...
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
...
'PDF' => Barryvdh\DomPDF\Facade::class,
]
Step 5: Create Route
Add route code in the routes/web.php file.
#routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Auth::routes();
Route::get('/jobsheet','JobsheetController@pdf');
?>
Step 6: Create Model and Controller
Here run below command help to create the controller and model.
php artisan make:model Jobsheet -r
Add model code in the app/Jobsheet.php file.
#app/Jobsheet.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Jobsheet extends Model
{
protected $fillable = [
'company_name','dates','job_quantity','size','papers_and_colors','quantity_and_size','note','job_name','color_of_ink','numbering','rulling','punching','perphoration','fixed_copy','type_of_binding'
];
}
?>
Add controller code in the app/Http/Controllers/JobsheetController.php file.
#app/Http/Controllers/JobsheetController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Jobsheet;
use Response;
use PDF;
class JobsheetController extends Controller
{
public function pdf(Request $request)
{
$jobsheet = Jobsheet::all();
$pdf_name = time().'_devnote.pdf';
$pdf = PDF::loadView('devnote.pdf', compact('jobsheet'));
$pdf->save(storage_path('/').$pdf_name);
return $pdf->download($pdf_name);
}
}
?>
Step 7: Create Blade Files
Now, create a pdf.blade.php file in the resources/views/jobsheet/ folder directory and paste the below code.
#resources/views/jobsheet/pdf.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ config('app.name', 'Devnote') }} | PDF </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="en-us">
<style type="text/css">
html,body {
font-family: sans-serif;
}
.col-md-3 {
width: 30%;
float: left;
}
.col-md-6 {
width: 48%;
float: left;
}
.col-md-8 {
width: 60%;
float: left;
}
.col-lg-12 {
width: 100%;
float: left;
}
.col-md-12 {
width: 100%;
}
.text-center {
text-align: center;
}
.text-right {
text-align: right;
}
.text-uppercase {
text-transform: uppercase;
}
</style>
</head>
<body>
<div>
<div class="text-center">
<h1>Devnote</h1>
<p class="text-uppercase">How to create dompdf in laravel</p>
<div style="border: 1px solid black;"></div>
<span>Work Order</span>
<div style="border: 1px solid black;"></div>
<br>
</div>
<div class="col-md-6">
<strong>Job Number : </strong> 1690
</div>
<div class="col-md-6 text-right">
<strong>Date : </strong> 31-10-2019
</div>
<br><br>
<div class="col-md-12">
<strong>Company Name : </strong> Devnote Blog
</div>
<br>
<div class="col-md-12">
<strong>Job Name : </strong> Web Developer
</div>
<br>
<div class="col-md-6">
<strong>Job Quantity : </strong> 1000 blogs x 50 pages
</div>
<div class="col-md-6 text-right">
<strong>Size : </strong> 1/12 Blog
</div>
<br><br>
<div class="col-md-12">
<strong>Papers And Colors Of Papers : </strong>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div class="col-md-12">
<strong>Quantity And Size to run on machine : </strong>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
<div class="col-md-6">
<strong>Color of Ink : </strong> Yellow
</div>
<div class="col-md-6 text-right">
<strong>Numbering : </strong> 1600 to 16000
</div>
<div class="col-md-12"> </div>
<br>
<div class="col-md-6">
<strong>Rulling : </strong> NA
</div>
<div class="col-md-6 text-right">
<strong>Punching : </strong> NA
</div>
<div class="col-md-12"> </div>
<br>
<div class="col-md-6">
<strong>Perphoration : </strong> NA
</div>
<div class="col-md-6 text-right">
<strong>Fixed Copy : </strong> NA
</div>
<br><br>
<div class="col-lg-12">
<strong>Type of Binding : </strong> NA
</div>
<br><br>
<div class="col-lg-12">
<strong>Special Note : </strong>
<p></p>
</div>
<br><br>
<div class="col-md-8">
<strong>Prepared By </strong>
<br><br>
<div style="border: 1px solid black;"></div>
</div>
<div class="col-md-3" style="margin-left: 2%;">
<strong>Job Done By </strong>
<br><br>
<div style="border: 1px solid black;"></div>
</div>
<br><br><br><br>
<div class="col-lg-12">
<p>Developed by <a href="https://devnote.in/">devnote.in</a></p>
</div>
</div>
</body>
</html>
Now, run example using the below command.
php artisan serve
Open your browser and run the below url :
http://127.0.0.1:8000/jobsheet
Demo pdf : jobsheet.pdf
You need your laravel project without php artisan serve command.
Also Read : How to run laravel without php artisan serve command.
Note: if you install dompdf and No such file or directory by using laravel-dompdf package something error display.
Also Read : No such file or directory by using laravel-dompdf package