laravel 11 generate pdf and send email Check our tools website Word count
laravel 11 generate pdf and send email Check our tools website Word count

Laravel 11 Generate PDF and Send Email Tutorial

In this tutorial, we’ll walk through how to generate a PDF and send it via email using Laravel 11. Laravel makes these tasks straightforward with its rich set of tools and libraries. We’ll use the barryvdh/laravel-dompdf package for generating PDFs and Laravel’s built-in mail functionality to send emails.

Prerequisites

Before we start, make sure you have the following:

  • Laravel 11 installed
  • Basic knowledge of Laravel
  • Composer installed

Step 1: Setting Up Laravel Project

If you haven’t set up a Laravel project yet, you can create a new one by running:

composer create-project --prefer-dist laravel/laravel laravel-pdf-email-tutorial

Navigate to the project directory:

cd laravel-pdf-email-tutorial

Step 2: Install DomPDF Package

We will use the barryvdh/laravel-dompdf package to generate PDFs. Install it using Composer:

composer require barryvdh/laravel-dompdf

After installing the package, register the service provider in the config/app.php file:

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

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

Step 3: Create a Route for Generating PDF

We’ll create a simple route to generate and view a PDF. Open the routes/web.php file and add the following route:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PDFController;

Route::get('/generate-pdf', [PDFController::class, 'generatePDF']);

Step 4: Create PDFController

Next, we need to create a controller to handle the PDF generation logic. Run the following command to generate a controller:

php artisan make:controller PDFController

Open the newly created PDFController.php and add the following code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller
{
    public function generatePDF()
    {
        $data = [
            'title' => 'Welcome to Laravel PDF Tutorial',
            'date' => date('m/d/Y'),
        ];

        $pdf = PDF::loadView('pdf.myPDF', $data);

        return $pdf->download('tutorial.pdf');
    }
}

Step 5: Create a View for the PDF

Create a view that will be converted to a PDF. In the resources/views directory, create a new folder named pdf and a file named myPDF.blade.php:

mkdir resources/views/pdf
touch resources/views/pdf/myPDF.blade.php

Open the myPDF.blade.php file and add the following HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>Generated on {{ $date }}</p>
</body>
</html>

Step 6: Sending the PDF via Email

To send the generated PDF via email, we need to create a mailable class. Run the following command:

php artisan make:mail PDFMail

Open the PDFMail.php file in the app/Mail directory and modify it as follows:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use PDF;

class PDFMail extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        $data = [
            'title' => 'Welcome to Laravel PDF Tutorial',
            'date' => date('m/d/Y'),
        ];

        $pdf = PDF::loadView('pdf.myPDF', $data);

        return $this->view('emails.pdf')
                    ->subject('Your PDF is ready')
                    ->attachData($pdf->output(), 'tutorial.pdf');
    }
}

Step 7: Create an Email View

Create an email view that will be used to send the PDF. In the resources/views/emails directory, create a file named pdf.blade.php:

mkdir resources/views/emails
touch resources/views/emails/pdf.blade.php

Open the pdf.blade.php file and add the following HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>PDF Email</title>
</head>
<body>
    <h1>Your PDF is attached</h1>
    <p>Please find the attached PDF document.</p>
</body>
</html>

Step 8: Create a Route for Sending the Email

Finally, create a route to send the email. Open the routes/web.php file and add the following route:

use App\Mail\PDFMail;
use Illuminate\Support\Facades\Mail;

Route::get('/send-pdf-email', function () {
    Mail::to('recipient@example.com')->send(new PDFMail());
    return 'Email has been sent!';
});

Replace recipient@example.com with the actual recipient’s email address.

Conclusion

That’s it! You’ve successfully created a Laravel application that generates a PDF and sends it via email. You can customize the PDF content and email view as per your requirements.

To test the functionality, you can run the Laravel development server:

php artisan serve

Visit http://localhost:8000/generate-pdf to download the PDF, and http://localhost:8000/send-pdf-email to send the PDF via email.

Feel free to ask if you have any questions or run into any issues!