How To Generate PDF File In Laravel 10

Websolutionstuff | Mar-10-2023 | Categories : Laravel

In this article, we will see how to generate a pdf file in laravel 10. Here, we will learn about laravel 10 generating pdf files using dompdf. For generating pdf files we will use the laravel-dompdf package. It is very easy to generate pdf in laravel 10.

Using barryvdh/laravel-dompdf pdf you can create a new DomPDF instance and load an HTML string, file, or view name. You can save it to a file, or show it in the browser or download it. Also, you can generate pdf from HTML view in laravel 10.

So, let's see generate pdf in laravel 10, laravel 10 generate pdf files using dompdf, how to generate pdf files using dompdf in laravel 10, laravel 10 pdf generator, download pdf in laravel 10, and how to generate pdf from html view in laravel 10.

 Step 1: Install Laravel 10

In this step, we will install laravel 10 using the following command.

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

 

 

Step 2: Install barryvdh/laravel-dompdf Package

In this step, we will install barryvdh/laravel-dompdf Package using the composer command.

composer require barryvdh/laravel-dompdf

 

Step 3: Create Controller

Now, we will create a PDFController using the following command.

php artisan make:controller PDFController

app/Http/Controllers/PDFController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use PDF;

class PDFController extends Controller
{    
    public function index(Request $request)
	{
	    $users = User::get();

	    $data = [
	            'title' => 'Welcome to Websolutionstuff',
	            'date' => date('d/m/Y'),
	            'users' => $users
	    ];

	    if($request->has('download'))
	    {
	        $pdf = PDF::loadView('index',$data);

	        return $pdf->download('users.pdf');
	    }

	    return view('index',compact('users'));
	}
}

 

 

Step 5: Add Route

Now, we will add routes to the web.php file.

routes/web.php 

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\PDFController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::resource('users', PDFController::class);

 

Step 6: Create Blade File

In this step, we will create an index.blade.php file for download and generate a pdf file.

resources/views/index.blade.php

<!DOCTYPE html>
<html>
	<head>
	<title>How To Generate PDF File In Laravel 10 - Websolutionstuff</title>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	</head>
	<body>
		<h1>{{ $title }}</h1>
		<p>{{ $date }}</p>
		<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
		<div class="container">
			<div class="row">
			<div class="col-lg-12" style="margin-top: 15px">				
				<div class="pull-right">
					<a class="btn btn-primary" href="{{route('users.index',['download'=>'pdf'])}}">Download PDF</a>
				</div>
			</div>
			</div><br>
			<table class="table table-bordered">
			<tr>
				<th>ID</th>
				<th>Name</th>
				<th>Email</th>
			</tr>
			@foreach($users as $user)
			<tr>
				<th>{{ $user->id }}</th>
				<td>{{ $user->name }}</td>
				<td>{{ $user->email }}</td>
			</tr>
			@endforeach
			</table>
		</div>
	</body>
</html>

Output:

how_to_generate_pdf_file_in_laravel_10_output

 


You might also like:

Recommended Post
Featured Post
How To Add Toastr Notification In Laravel
How To Add Toastr Notification...

In this tutorial, I will show you how to add toastr notification in laravel application. There are many types of no...

Read More

May-29-2020

How to Install Composer on Ubuntu 22.04
How to Install Composer on Ubu...

Hey there! If you're diving into the world of PHP development on your Ubuntu 22.04 machine, you'll likely come a...

Read More

Jan-10-2024

How To Validate Phone Number Using Jquery Input Mask
How To Validate Phone Number U...

In this small tutorial, I will explain to you how to validate phone numbers using jquery input mask, Using jqu...

Read More

Jun-12-2020

Top 20 Best Javascript Tips and Tricks
Top 20 Best Javascript Tips an...

Welcome to the fascinating world of JavaScript, where innovation and creativity converge to deliver dynamic and interact...

Read More

Aug-14-2023