Laravel

How to generate a PDF File From Laravel view

In this example, I’ll show you how to generate a PDF file in Laravel from an HTML page. I’ll use the laravel-dompdf package to create PDF files, and it also has download file functionality.

I’ll show you how to make PDF files and download them to your system in a very simple method. In Laravel, we utilize the dompdf package to generate pdf files from HTML views.

You can also check another tutorial on Export Data with PHP:

What Is Dompdf Liberary ?

Dompdf is a laravel package that converts HTML to PDF using Laravel. The dompdf is a PHP-based HTML layout and rendering engine that is (largely) compliant with CSS 2.1.

It’ll download and read external stylesheets, inline style tags, and all the style attributes of individual HTML elements. It also supports the majority of HTML presentational attributes.

How To Generate a PDF in Laravel

Let’s integrate the Dompdf package with laravel and generate pdf file.

I am assuming, You have laravel application, if not please create an app using the below command:

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

Step 1: Install The Laravel-Dompdf Package
With the help of a composer, you can easily install the dependencies of laravel-dompdf package. Please run the below command inside your laravel project:

laravel-app-path> composer require barryvdh/laravel-dompdf

Step 2: Configure Laravel-Dompdf Package
Just go to Config/App.php and create these entries in providers and aliases.

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

Step 3: Create A Blade File
Inside the resources >> views folder, We’ll create a new file called index.blade.php.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script></pre>
<div class="container">
<div class="row">
<div class="col-lg-12" style="margin-top: 15px;">
<div class="pull-left">
<h2>Generate And Download PDF File Using dompdf</h2>
</div>
<div class="pull-right"><a class="btn btn-primary" href="{{route('emp.index',['download'=>'pdf'])}}">Download PDF</a></div>
</div>
</div>
@foreach ($emps as $emp) @endforeach
<table class="table table-bordered">
<tbody>
<tr>
<th>Id</th>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>{{ $emp->id }}</td>
<td>{{ $emp->name }}</td>
<td>{{ $emp->salary }}</td>
</tr>
</tbody>
</table>
</div>

Step 4: Create Controller
Now create a controller on this path app\Http\Controllers\EmployeeController.php and using the below command.

php artisan make:controller EmployeeController

Added the below code into this file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Employee;
use PDF;

class EmployeeController extends Controller
{
    
    public function index(Request $request)
    {
        $emps = Employee::latest()--->paginate(2);
  
        if($request->has('download'))
        {
            $pdf = PDF::loadView('emp.index',compact('emps'));
            return $pdf->download('pdfview.pdf');
        }

        return view('emp.index',compact('emps'));
    }
}

Step 5: Add Route path
Add Route information into the routes/web.php file.

Route::resource('emp','EmployeeController');

Step 6: Run Laravel App
Now, run the laravel project using the below command.

php artisan serve

open the below URL on your browser.

http://localhost:8000/emp

References:

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories