How to Generate PDFs and Sending Email in Laravel | websolutioncode.com
How to Generate PDFs and Sending Email in Laravel | websolutioncode.com

How to Generate PDFs and Sending Email in Laravel

Introduction:

Laravel, a popular PHP web application framework, provides developers with a powerful set of tools for building robust and scalable applications. In this article, we will explore how to Generate PDFs and Sending Email in Laravel. We’ll walk through the process step by step, providing practical code examples along the way.

Requirements:

Before we start, ensure you have the following prerequisites:

  1. Laravel installed on your machine.
  2. Composer installed to manage dependencies.
  3. A working mail setup in your Laravel application.

Step 1: Install Required Packages

To Generate PDFs and Sending Email in Laravel, we need to install two packages: barryvdh/laravel-dompdf for PDF generation and Laravel’s built-in swiftmailer for email functionality.

Run the following commands in your terminal:

composer require barryvdh/laravel-dompdf
composer require illuminate/mail

Step 2: Configure Dompdf

After installing the packages, add the service provider and alias for Dompdf in the config/app.php file:

// config/app.php

'providers' => [
    // ...
    Barryvdh\DomPDF\ServiceProvider::class,
],

'aliases' => [
    // ...
    'PDF' => Barryvdh\DomPDF\Facade::class,
],

Step 3: Create a Controller

Generate a controller to handle Generate PDFs and Sending Email in Laravel:

php artisan make:controller PdfController

Edit the generated controller (app/Http/Controllers/PdfController.php) with the following code:

// app/Http/Controllers/PdfController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;
use Mail;

class PdfController extends Controller
{
    public function generateAndSendPDF()
    {
        $data = ['name' => 'John Doe']; // Customize your data

        $pdf = PDF::loadView('pdf.template', $data);
        $pdfContent = $pdf->output();

        Mail::send('emails.pdf', ['name' => $data['name']], function ($message) use ($pdfContent) {
            $message->to('recipient@example.com')
                    ->subject('Subject of the email')
                    ->attachData($pdfContent, 'document.pdf');
        });

        return "PDF generated and sent successfully!";
    }
}

Step 4: Create Blade Views

Create a blade view for the PDF content (resources/views/pdf/template.blade.php):

<!-- resources/views/pdf/template.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF Document</title>
</head>
<body>
    <h1>Hello, {{ $name }}</h1>
    <!-- Add your PDF content here -->
</body>
</html>

Create another blade view for the email (resources/views/emails/pdf.blade.php):

<!-- resources/views/emails/pdf.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email with PDF Attachment</title>
</head>
<body>
    <p>Dear {{ $name }},</p>
    <p>Please find the attached PDF document.</p>
</body>
</html>

Step 5: Define Routes

Update your web.php routes file (routes/web.php) to include the route to the controller method:

// routes/web.php

use App\Http\Controllers\PdfController;

Route::get('/generate-pdf-and-send-email', [PdfController::class, 'generateAndSendPDF']);

Step 6: Run Your Application

Finally, run your Laravel application using the following command:

php artisan serve

Visit http://localhost:8000/generate-pdf-and-send-email in your browser, and you should see a success message.

Conclusion:

Congratulations! You have successfully implemented a Laravel application that generates PDFs and sends them via email. This step-by-step guide and accompanying code examples should help you integrate this functionality into your projects with ease. Feel free to customize the code according to your specific requirements. Happy coding!

Check our tools website Word count
Check our tools website check More tutorial

Leave a Reply