Contact US


Get Your Project Done by our experts
info@fundaofwebit.com

How to create html template and generate PDF in laravel 5.8

Whatsapp

https://fundaofwebit.com/post/how-to-create-html-template-and-download-pdf-in-laravel-5-8

Laravel 5.8 - How to Generate HTML to PDF with Laravel domPDF


In this post, we are going to learn about how to generate PDF in laravel 5.8 where i am using a simple html template to convert into PDF in laravel 5.8.

To generate HTML to PDF with laravel-domPDF, I am using a package called barryvdh/laravel-dompdf 

So, Let's get started:

Step 1: Download or install the laravel 5.8 version using the following command:

$ composer create-project --prefer-dist laravel/laravel blog "5.8.*"


Step 2: Now, we have to install the package laravel-dompdf Package with the following command:

$ composer require barryvdh/laravel-dompdf

After installing the laravel-dompdf package successfully, setup with below steps. 


Step 3: Open the app.php file in the following path: config/app.php. Add service provider and alias as shown below:

'providers' => [

    Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [

    'PDF' => Barryvdh\DomPDF\Facade::class,
],


Step 4: Create a Controller named as PdfgenerateController using the following command: 

$ php artisan make:controller PdfgenerateController

After creating the controller successfully, paste the below code in the PdfgenerateController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfgenerateController extends Controller
{
    public function generatePDF()
    {
        $data = [
            'heading' => 'Welcome to Funda of Web IT',
            'description' => 'This description of Funda of Web IT'
        ];
        $pdf = PDF::loadView('yourPDF'$data);

        return $pdf->download('fundaofwebit.pdf');
    }

}

 

Step 5: Let's create a route to generate the pdf and connect with the above created Controller as shown below:

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


Step 6: Final step, now we will create a file named yourPDF.blade.php in the following path: resource/views/yourPDF.blade.php and put the following code:

<!DOCTYPE html>
<html>
<head>
    <title>Funda of Web IT</title>
</head>
<body>
    <h1>Hey, {{ $heading }}</h1>
    <h5>{{ $description }}</h5>
    <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.
    </p>
</body>
</html>


Thanks for reading..

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT