In this tutorial, we'll explore how to generate PDF files in Laravel 10 using the barryvdh/laravel-dompdf package. Generating PDF files is a common requirement in web applications, and with DomPDF, it becomes straightforward in Laravel 10.
With the barryvdh/laravel-dompdf package, you can create a new DomPDF instance within your Laravel application. You have the flexibility to save the generated PDF to a file, display it in the browser, or prompt the user to download it.
So, let's see how to generate pdf files using dompdf in laravel 10, laravel 10 generate pdf files using dompdf, how to create pdf files in laravel 8/9/10, and how to convert html to pdf in laravel 10.
Start by installing Laravel 10 with the following command:
composer create-project --prefer-dist laravel/laravel laravel_10_pdf
barryvdh/laravel-dompdf
PackageNext, install the barryvdh/laravel-dompdf
package using Composer:
composer require barryvdh/laravel-dompdf
Generate a new controller named PDFController
using the command:
php artisan make:controller PDFController
Then, add the following code to the PDFController.php
file:
<?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 vidvatek.com',
'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'));
}
}
In the routes/web.php
file, add the following route:
use App\Http\Controllers\PDFController;
Route::resource('users', PDFController::class);
Now, create a blade file named index.blade.php
in the resources/views
directory and populate it with the following code:
<!DOCTYPE html>
<html>
<head>
<title>How to Generate PDF File using DomPDF in Laravel 10 - Vidvatek.com</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>
With this setup, you can easily generate PDF files in Laravel 10 using DomPDF. Users can download the generated PDF containing user information from the specified route.
You might also like:
- Read Also: Delete Multiple Records using Checkbox in Laravel 10
- Read Also: How to Send Email with PDF Attachment in Laravel 10
- Read Also: Convert HTML to PDF in Python: Step-by-Step Guide
- Read Also: Laravel 10 Import and Export CSV and Excel Files