How Generate PDF From HTML In Laravel dompdf

In this post i explain how to create PDF From HTML in laravel using dompdf library. Laravel dompdf  library you can easy to generate dynamic PDF in your application.

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.

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' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
  
'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.

Route::get('generate-pdf','HomeController@generatePDF');

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.

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

Step 5: Create Blade File

In this step

Read Also :  Laravel Migration Add Comment to Column Example

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

<!DOCTYPE html>
<html>
<head>
    <title>Hi</title>
</head>
<body>
    <h1>Welcome to web-tuts.com - {{ $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>

I hope you understand laravel dompdf library and it can help you…