Laravel 10 Generate PDF File using DomPDF Example

HTML to PDF conversion: With DomPDF, you can convert HTML content, including CSS styles and images, into PDF format. This allows you to leverage your existing HTML templates and views to generate professional-looking PDF documents.

Step 1: Install Laravel App

composer create-project laravel/laravel example-GeneratePdf

Step 2: Install DomPDF Package

composer require barryvdh/laravel-dompdf

Step 3: Create Controller

php artisan make:controller PDFController

Add dummy data

php artisan tinker


    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Models\User;
    use PDF;
    class PDFController extends Controller
         * Display a listing of the resource.
         * @return \Illuminate\Http\Response
        public function generatePDF()
            $users = User::get();
            $data = [
                'title' => 'Welcome to',
                'date' => date('m/d/Y'),
                'users' => $users
            $pdf = PDF::loadView('myPDF', $data);
            return $pdf->download('itsolutionstuff.pdf');

Step 4: Add Route

    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::get('generate-pdf', [PDFController::class, 'generatePDF']);

Step 5: Create View File


<!DOCTYPE html>
        <title>Laravel 10 Generate PDF Example -</title>
        <link rel="stylesheet" href="" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <h1>{{ $title }}</h1>
        <p>{{ $date }}</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua.</p>
        <table class="table table-bordered">
            @foreach($users as $user)
                <td>{{ $user->id }}</td>
                <td>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>

Run Laravel App:

php artisan serve

Now, Go to web browser, type the given URL and see the output: