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



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

	        $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.


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.


<!DOCTYPE html>
	<title>How To Generate PDF File In Laravel 10 - Websolutionstuff</title>
	<link rel="stylesheet" href="">
	<script src=""></script>
		<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>
			<table class="table table-bordered">
			@foreach($users as $user)
				<th>{{ $user->id }}</th>
				<td>{{ $user->name }}</td>
				<td>{{ $user->email }}</td>




You might also like:

Recommended Post
Featured Post
Laravel 10 Create AJAX Pagination Example
Laravel 10 Create AJAX Paginat...

In this article, we will see laravel 10 create an ajax pagination example. Here, we will learn about how to create...

Read More


How to Clear Form Data Using jQuery
How to Clear Form Data Using j...

In this small tutorial we will see how to clear form data using we will reset form using jquery. many t...

Read More


Multi Step Form Wizard jQuery Validation
Multi Step Form Wizard jQuery...

In this article, we will see multi step form wizard jquery validation. Here, we will learn jquery validation for mu...

Read More


How To Install PHP XML Extension In Ubuntu
How To Install PHP XML Extensi...

In this article, I will guide you through the process of installing the PHP XML extension in Ubuntu. The PHP XML extensi...

Read More