PDF Generate using Laravel 7

pdf download

Hello Friends, Today I will teach how to Download PDF using Laravel 7.

Basically, PDF is using For Ecommerce, Financial and another firm for downloading for invoices and another thing.

So, Let’s Start our tutorial.


Output:


Step 1: Install-Package of PDF.

Laravel 7 will not provide PDF Download directly. So, We have to Install the package Composer in Laravel 7.

Open Your Terminal and put the below Code.

composer require barryvdh/laravel-dompdf

After Successfully install the package now move to the next step.


Step 2: Changes in Config File.

Now Open your config/app.php file and put the below code.

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

Step 3: Create a Route

Open web.php and put the below code.

Route::get('/downloadpdf','PDFController@downloadpdf')->name('downloadpdf');

Step 4: Changes in Controller.

Now create PDFController.php and put the below code.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller{
    public function downloadpdf(){
        $data = ['name' => 'Dhaval Shah! How are you?'];
        $pdf_view = PDF::loadView('download-pdf', $data);
        return $pdf_view->download('download.pdf');
    }
}

Step 5: Create a blade file.

Create a new blade file pdf.blade.php and put the below code.

<!DOCTYPE html>
<html>
  <body>
    <h1>Laravel 7 Generate PDF Tutorial</h1>
    <p>The Portable Document Format (PDF) (redundantly: PDF format) is a file format developed by Adobe in the 1990s to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.</p>
  </body>
  </html>

Now Open the browser and put the below code:

http://localhost:8000/downloadpdf

I hope it will help you!

About Dhaval Shah

My name is Dhaval Shah. I'm a Laravel developer. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress, and Bootstrap.
Follow me on Github / Linkedin

Leave a Reply

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

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!