How To Generate A PDF from HTML in Laravel 11

Hello ,
I was trying to generate a PDF payment receipt for my SAAS application and when i search for a method, i found a simple solution.
Here i am sharing how i able to make it work.

can generate and download a PDF receipt in Laravel using the dompdf/dompdf package. First, make sure you have the barryvdh/laravel-dompdf package installed. You can install it via Composer:

composer require barryvdh/laravel-dompdf

 

Now, create a route in your web.php file:

you can add the function to any of your controller but here in reference i use ReceiptController

use App\Http\Controllers\ReceiptController;

Route::get('/receipt/{id}', [ReceiptController::class, 'generateReceipt']);

 

Inside your controller you can use this code

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class ReceiptController extends Controller
{
    public function generateReceipt($id)
    {
        // Fetch payment details from the database using the ID
        // For example, assuming you have a Payment model:
        // $payment = Payment::find($id);

        // Dummy data for the example
        $payment = [
            'id' => $id,
            'name' => 'Ajith',
            'amount' => 5000,
            'date' => date('Y-m-d'),
            'description' => 'Payment for services rendered'
        ];

        // Generate PDF
        $pdf = PDF::loadView('receipt', compact('payment'));

        // Download PDF
        return $pdf->download('receipt.pdf');
    }
}

Next, create the receipt.blade.php view file in the resources/views directory:

<!DOCTYPE html>
<html>
<head>
    <title>Receipt</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .container {
            width: 80%;
            margin: 0 auto;
        }
        .header, .footer {
            text-align: center;
        }
        .content {
            margin-top: 20px;
        }
        .details {
            width: 100%;
            border-collapse: collapse;
        }
        .details th, .details td {
            border: 1px solid #000;
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Receipt</h1>
            <p>Receipt ID: {{ $payment['id'] }}</p>
        </div>
        <div class="content">
            <table class="details">
                <tr>
                    <th>Name</th>
                    <td>{{ $payment['name'] }}</td>
                </tr>
                <tr>
                    <th>Amount</th>
                    <td>{{ $payment['amount'] }}</td>
                </tr>
                <tr>
                    <th>Date</th>
                    <td>{{ $payment['date'] }}</td>
                </tr>
                <tr>
                    <th>Description</th>
                    <td>{{ $payment['description'] }}</td>
                </tr>
            </table>
        </div>
        <div class="footer">
            <p>Thank you for your payment!</p>
        </div>
    </div>
</body>
</html>

 

This code sets up a basic receipt generation and download system in Laravel. When you access the /receipt/{id} route, it will generate a PDF receipt based on the provided payment details and download it. Make sure to replace the dummy payment data with actual data from your database as needed.

 

 

Ajith Jojo Joseph

Hire Me for Freelancing $30/Hour ,
Contact via email : [email protected]

Share with your friends:

Comments are closed.

How To Generate A PDF from HTML in Laravel 11

Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]

June 22, 2024

How to create Laravel Flash Messages

Laravel flash messages are a convenient way to display one-time notifications to the user after a form submission or other […]

December 22, 2022

How to make Custom Artisan Command in Laravel

Custom Artisan commands are a useful feature of Laravel that allow you to define your own command-line commands for tasks […]

December 22, 2022

Tips for Laravel migrations

Laravel migrations are a powerful tool for managing and modifying your database schema in a structured and organized way. Here […]

December 22, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now