Laravel Generate PDF File Tutorial

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this tutorial, I will show you how to generate pdf file in laravel application. Here i will explain to how to generate pdf file by using the dompdf package .So here i can generate pdf file from view or html blade file in laravel application.

We know PDF is most using in any level of project. PDF is one of the basic requirement when we are working with any erp level project or any e commerce website. So we may need to create the pdf file for report or invoice etc.

So now here i will give you full example of how to generate pdf file with example step by step like create laravel project,controller, route, blade file etc. So you need to just follow few steps.

Step 1 : Install Laravel Application

Here i am going from scratch, So i need to get a fresh Laravel application by using the bellow command, So open your terminal OR command prompt and then need run bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step: 2 Install dompdf Package

So here in this step need to install the dompdf Package by using bellow command

composer require barryvdh/laravel-dompdf

Add providers and aliases

after the install package then need to add providers and aliases in the “config/app.php” file.

following path: config/app.php

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

So now in this is step i need to create a route for generate pdf layout file

following path:/routes/web.php

Route::get('pdf-generate','PdfGenerateController@PDFgenerate');

Step 4: Create Controller

Now here in this step i should create a new controller as PdfGenerateController, So need to run bellow command for generate new controller

php artisan make:controller PdfGenerateController

Now in this step, this controller will manage to Generate PDF File layout in the bellow content in controller file. Just need to following file path

following path:/app/Http/Controllers/PdfGenerateController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PdfGenerateController extends Controller
{
    public function PDFgenerate()
    {
        $data = ['title' => 'NiceSnippets Blog'];
        $pdf = PDF::loadView('ourPDF', $data);
  
        return $pdf->download('Nicesnippets.pdf');
    }
}
Step 5: Create View File

So now we are in the last step, So let’s create ourPDF.blade.phpfor layout of pdf file and then put following code

following path:/resources/views/myPDF.blade.php

<!DOCTYPE html>
<html>
<head>
	<title>CodingsPoint</title>
</head>
<body>
  <h1>Welcome to codingspoint.com - {{ $title }}</h1>
  <p>codingspointBlog provides you latest Code Tutorials on PHP, Laravel, Codeigniter,
  JQuery, Node js, React js, Vue js, PHP, and Javascript. Mobile technologies like Android,
  React Native, Ionic etc.</p>
</body>
</html>

Now finally we are ready to run our example so need to run bellow command for quick run:

php artisan serve

Now we can open bellow URL on your browser:

http://localhost:8000/pdf-generate

Read Also : Laravel Eloquent whereRaw Example

Thank you read.Hope it will help you. Also follow on facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →