How Generate PDF From HTML In Laravel dompdf

Hi friends, Today you will learn Generate PDF From HTML using Laravel. So you are following my step to generate pdf from html in easy way.

Laravel Dompdf is most popular library for generate PDF. In this example we will use laravel dompdf ( barryvdh/laravel-dompdf package ) for generate PDF file in laravel application.

PDF is one of the basic requirement when you create ecommerce website or application. Also we need to create report or invoice ect.

In this example we install laravel domdpf (  barryvdh/laravel-dompdf ) library using composer. So let’s following step to create dompdf in your laravel application.

Laravel Generate PDF From HTML

Step 1: Install Laravel

In this step we will install fresh laravel package for generate PDF from HTML. So following to install fresh laravel package:

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

Step 2: Install laravel-dompdf Library

In this step we will install laravel barryvdh/laravel-dompdf composer package by using the following command:

composer require barryvdh/laravel-dompdf

After install the laravel dompdf library open config/app.php file and add service provider and alias.

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

Step 3: Create Routes

In this is step we need to create routes for items listing. So open your routes/web.php file and add following route.


Step 4: Create Controller

Now in this step we need to create new controller HomeController that will manage generatePDF method of route. So let’s put bellow code.

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' => 'Welcome to'];
        $pdf = PDF::loadView('myPDF', $data);
        return $pdf->download('web-tuts.pdf');

Step 5: Create Blade File

In this step

Read Also :  Laravel Include Blade File with Data Example

In Last step create myPDF.blade.php(resources/views/myPDF.blade.php) for layout of pdf file and put following code:

<!DOCTYPE html>
    <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>

I hope you understand Laravel Pdf Generator Using Dompdf and it can help you…

Read Also :  How to Send SMS using Twilio in Laravel