Surfe.be - passive income

How to create and Download PDF in Laravel

How to Create and Download PDF

In this example, i would like to share with you how to generate pdf file from view or html or blade using dompdf library in laravel 5.8. we can easily create pdf file for invoice, report, data etc and also can give for download in laravel 5.8.As you know, few days ago laravel 5.8 released. so i plan to add tutorial for creating pdf file with laravel 5.8.PDF is one of basic requirement when you are working with erp level project or e commerce website. we may need to create pdf file for report or invoice etc. So, here i will give you very simple example for create pdf file with laravel.You need to just follow bellow step to create pdf file and also can download. So let's do bellow steps.

how to create and download pdf in laravel

Step 1: Install Laravel 5.8

I am going to explain step by step from scratch so, we need to get fresh Laravel 5.8 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 laravel-dompdf Package

first of all we will install barryvdh/laravel-dompdf composer package by following composer command in your laravel 5.8 application.

composer require lynx39/lara-pdf-merger

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php

'providers' => [
	....
	LynX39\LaraPdfMerger\PdfMergerServiceProvider::class,
],

'aliases' => [
	....
	'PdfMerger' => LynX39\LaraPdfMerger\Facades\PdfMerger::class,
]

Read also How to upload image in laravel

In this is step we need to create routes for items listing. so open your "routes/web.php" file and add following route.

routes/web.php

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

Step 3: Create Controller

Here,we require to create new controller HomeController that will manage generatePDF method of route. So let's put bellow code.

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;

class HomeController extends Controller
{

    public function generatePDF()

    {

        $data = ['title' => 'Welcome to techmahedy.com'];
        $pdf = PDF::loadView('myPDF', $data);
        return $pdf->download('laramust.pdf');

    }

}

Step 4: Create View File

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

resources/views/myPDF.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Download with DOM PDF</title>
</head>

<body>

	<h1>Welcome to laramust.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>

Hope it will help you. Here i just showed you the procedure of how you can implent it. That’s it. If you like this tutorial please don’t forget to share.

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST