Laravel 7 Generate PDF From View Example Tutorial

10-Apr-2023

.

Admin

Laravel 7 Generate PDF From View Example Tutorial

Hi Guys

In this tutorial, you will learn how to create pdf in laravel 7. step by step explain laravel 7 create pdf from view. This article will give you simple example of save as pdf in laravel 7.

we will help you to give example of laravel 7 pdf generator. Alright, let’s dive into the steps.Type a message

Here i give you full example of generate pdf file example step by step like create laravel 7 project,controller, route, blade file etc. So you have to just follow few steps.

Step 1 : Install Laravel 7 Application


We are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Step: 2 Install dompdf Package

Here in this step the dompdf Package

composer require barryvdh/laravel-dompdf

Add providers and aliases

After the install package 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

In this is step we need to create route for generate pdf layout file

following path:/routes/web.php

Route::get('pdf-create','PdfController@create');

Step 4: Create Controller

Here this step now we should create new controller as PdfGenerateController,So run bellow command for generate new controller

php artisan make:controller PdfController

now this step, this controller will manage Generate PDF File layout bellow content in controller file.following fille path

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;

class PdfController extends Controller

{

public function create()

{

$data = ['title' => 'Laravel 7 Generate PDF From View Example Tutorial'];

$pdf = PDF::loadView('pdf', $data);

return $pdf->download('Nicesnippets.pdf');

}

}

Step 5: Create View File

In Last step, let's create pdf.blade.php for layout of pdf file and put following code

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

<! DOCTYPE html>

<html>

<head>

<title>Laravel 7 Generate PDF From View Example Tutorial - NiceSnippets</title>

</head>

<body>

<h1>Welcome to Nicesnippets.com - {{ $title }}</h1>

<p>NiceSnippets Blog 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 we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/pdf-create

Preview

It will help you...

#Laravel 7