Laravel 6 Tutorial - How to generate pdf file and download

Laravel 6 Tutorial - How to generate pdf file and download

In this post, I will explain how to generate pdf file and download in Laravel 6

Originally published at https://itsolutionstuff.com

I will explain step by step creating pdf file with Laravel 6 using dompdf package. Using dompdf package we can generate pdf file from view or html blade file in Laravel 6.

As you know, few days ago Laravel 6 released. So i plan to add tutorial for creating pdf file with Laravel 6.

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.

Step 1: Install Laravel 6

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

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

composer require barryvdh/laravel-dompdf

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

config/app.php

'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],

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

Read Also: Laravel 6 Release New Features and Upgrade

Step 3: Add Route

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','[email protected]');

Step 4: Add Controller

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

app/Http/Controllers/PDFController.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request; use PDF;

class PDFController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function generatePDF() { $data = ['title' => 'Welcome to ItSolutionStuff.com']; $pdf = PDF::loadView('myPDF', $data);

    return $pdf-&gt;download('itsolutionstuff.pdf');
}

}

Step 5: 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

Read Also: Laravel 6 Ajax Form Validation Tutorial

<!DOCTYPE html>
<html>
<head>
    <title>Hi</title>
</head>
<body>
    <h1>Welcome to ItSolutionStuff.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>

Now we are ready to run this example and check it...

I hope it can help you...

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

What's New in Laravel 6.0

Tutorial Laravel 6 with Docker and Docker-Compose

Laravel 6 Authentication Tutorial

Laravel 6 CRUD Application Tutorial



laravel php web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...