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
The Mix Manifest Does Not Exist Laravel
The Mix Manifest Does Not Exis...

In this article, we will see the mix manifest does not exist in laravel 8 and laravel 9. We will solve the mix...

Read More

Oct-28-2022

Laravel 9 Authentication Using Inertia JS
Laravel 9 Authentication Using...

In this article, we will see laravel 9 authentication using inertia js. Here, you can learn how to authenticat...

Read More

Dec-05-2022

Cron Job Scheduling In Laravel
Cron Job Scheduling In Laravel

In this article, we will see cron job scheduling in laravel. Many times we require to run some piece of code in a specif...

Read More

Sep-28-2020

Laravel 8 Artesaos SEOTools Tutorial
Laravel 8 Artesaos SEOTools Tu...

Hello Devs, In this tutorial we will learn how to use SEOTools in laravel 8. we will give you example of se...

Read More

Jun-07-2021