How To Generate PDF and Send Email In Laravel 8

In this article, we will see how to generate a pdf and send an email in laravel 8. For generating PDF files we will use the "laravel-dompdf" package also we will use "mailtrap" for email sending. dompdf creates a pdf file and also provides download file functionalities. it is very easy to generate pdf in laravel 8. Using this tutorial you can directly create an invoice pdf and send it to email in laravel 8.

So, let's see generate a pdf file and send mail in laravel 8.

Read More or Install Dompdf Package from Here: barryvdh / laravel-dompdf.

Step 1: Install Laravel 8

In this step, we will install the laravel application using the below command.

composer create-project --prefer-dist laravel/laravel laravel_8_pdf_email



Step 2: Install the dom-pdf package

Now, install barryvdh/laravel-dompdf package using the below command.

composer require barryvdh/laravel-dompdf


Step 3: Set mail configuration

Now, open the .env file and set the email configuration. Create a mailtrap account for sending mail.








Step 4: Add Route

In this step, we will add routes in your routes/web.php file.

Route::get('send_mail_pdf', [SendMailPDFController::class, 'sendMailWithPDF'])->name('send_mail_pdf');



Step 5: Add Controller

Now, we will create SendMailPDFController and add the below code for sending mail with attachments.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
use Mail;

class SendMailPDFController extends Controller
    public function sendMailWithPDF(Request $request)
        $data["email"] = "";
        $data["title"] = "Laravel 8 send email with attachment - Techsolutionstuff";
        $data["body"] = "Laravel 8 send email with attachment";

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

        Mail::send('pdf_mail', $data, function ($message) use ($data, $pdf) {
            $message->to($data["email"], $data["email"])
                ->attachData($pdf->output(), "test.pdf");

        echo "email send successfully !!";


Step 6: Create PDF View File

In this step, we will create the PDF view which will be attached to the email.


<!DOCTYPE html>
    <title>Laravel 8 Send Email With Attachment - Techsolutionstuff</title>
    <h3> Laravel 8 Send Email With Attachment </h3>

    <p> generate pdf and send mail in laravel 8 </p>

    <p> Thanks & Regards </p>


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.