How to create Pdf file from HTML in Laravel 7.0

Today I will share with you how to create pdf file from the HTML page. This is quite important for us when we are working on large project and we have to create payment proof or we are working on the eCommerce website we have to generate the pdf file.
I will tell you to step by step how to create Html to a pdf file and automatically download it.
In this example we have to install barryvdh/laravel-dompdf laravel package after this we will create route and controller.
Then we will make the pdf view file. After this, we will download it.You need just follow the below steps.

Step 1: Install Laravel 7
Step 2: Install laravel-dompdf PackageStep 3: Configure Package
Step 4: Create Routes
Step 4: Create Controller
Step 5: Create Blade File

Step 1: Install Laravel 7

In the next step you have to install laravel .For this purpose you have to run composer command. Just open command prompt and run the command.It will install laravel .

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

Step 2: Install laravel-dompdf Package

In the next step you have to install pdf laravel package.For this purpose you have to run composer command. Just open command promp and run the command

composer require barryvdh/laravel-dompdf 

Step 3: Configure Package

The next step is to add configuration in the config/app.php file. This will configure with the app of larval.For this purpose, you have to put the following code into the app. Php file

'providers' => [ 
Barryvdh\DomPDF\ServiceProvider::class, 
], 
'aliases' => [ 
.... 
'PDF' => Barryvdh\DomPDF\Facade::class, 
'providers' => [ 
.... 
Barryvdh\DomPDF\ServiceProvider::class, 
], 
'aliases' => [ 
.... 
'PDF' => Barryvdh\DomPDF\Facade::class, 
] 

Step 4: Create Routes

In this step, you have to create a route. For this open command prompt and run the comand.This will create a route.Then you have to create a controller and define the following function in it.

Route::get('generate-file','HomeController@generatepdf'); 

Step 4: Create Controller

Then you have to make view.create a view which you want to make pdf and then define it into the controller function.

<?php 

//controllers 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use PDF; 
class HomeController extends Controller 
{ 
   /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
   public function generatepdf() 
   { 

       $data = ['title' => 'Codespecialist']; 
       $pdf = PDF::loadView('pdf', $data); 
       return $pdf->download('codespecialist.pdf'); 
   } 
} 

Step 5: Create Blade File

You have to create blade file which you want to convert into pdf file.create pdf.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Code Specialist</title>
</head>
<body>
<h1>Welcome to {{$title}} </h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>

Create Pdf File By Form Filling

If you want to make pdf file by filling the form and show in the pdf file. For this purpose define a post route then define data you want to display. Define it into function. Define variables on the view page and download the pdf file.Then you have to follow these steps.

Step 1: Create Routes
Step 2: Create Controller Step 3: Create form
Step 4: Create Blade File

Step 1: Create Routes

For this, you have to first create a post route in the web.php .For this purpose define the following route in your web.php page as it is

Route::post('pdf_generate','HomeController@pdf_generate'); 

Step 2: Create Controller

Create a controller in which you have to define the function and variable of the request you want to print on the pdf.balde.php page. For this purpose copy the following code and paste it to your controller.

<?php 

//controllers 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use PDF; 
class HomeController extends Controller 
{ 
   /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
   public function pdf_generate(Request $request) 
   { 

       $data = [
'title' => 'Codespecialist',
'name' => $request->name,
'email' => $request->email,

]; 
       $pdf = PDF::loadView('pdf', $data); 
       return $pdf->download('codespecialist.pdf'); 
   } 
} 

Step 3: Create form

Next step is to make a form on home.blade.php and the fields you want to print on it.I am going to input name and email.This is the form code.

@extends('layout')

@section('content')
   <form>
  <div class="form-group">
    <label for="exampleInputName">Name</label>
    <input type="text" name="name" class="form-control" id="exampleInputEmail1" aria-describedby="namelHelp" placeholder="Enter Name">
  <label for="exampleInputEmail1">Email address</label>
    <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email"> 
  </div>
  <div class="form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>
@endsection

Step 4: Create Blade File

In the last step you have to create pdf.blade.php page to define the layout page of your pdf file.I am sharing my pdf file design

<!DOCTYPE html>
<html>
<head>
    <title>Hi</title>
</head>
<body>
<h1>CodeSpecialist.tech {{$title}} </h1>
<h3>Your Name: {{$name}} </h3>
<h3>Your Email Address: {{$email}} </h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>

I hope you learn well. If you have any confusion then ask me in the comment below I will answer it. For more tutorials and updates keep visiting code specialist.

Leave a Reply

Your email address will not be published. Required fields are marked *