Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF as mail attachment - Laravel

I want to create a PDF using the barryvdh/laravel-dompdf package and send this with an email as attachment.

The code I have now is:

$pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));

Mail::queue('emails.factuur', array('factuur' => $factuur), function($message) use ($pdf)
   {
       $message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
       $message->attach($pdf->output());
    });

But now I get the following error:

Serialization of 'Closure' is not allowed
like image 450
Keith666 Avatar asked Jun 23 '15 14:06

Keith666


People also ask

How do I convert HTML to PDF in laravel?

The Dompdf library can be used to convert HTML to PDF using PHP. The Dompdf is a PHP library that helps to convert HTML to PDF document. If your application is built with Laravel, the Dompdf library is the best option to create a PDF file and add HTML content to PDF document.


1 Answers

You can only send serializable entities to the queue. This includes Eloquent models etc. But not the PDF view instance. So you will probably need to do the following:

Mail::queue('emails.factuur', array('factuur' => $factuur), function($message)
   {
       $pdf = PDF::loadView('layouts.factuur', array('factuur' => $factuur));
       $message->to(Input::get('email'), Input::get('naam'))->subject('Onderwerp');
       $message->attach($pdf->output());
    });
like image 50
Luceos Avatar answered Sep 18 '22 16:09

Luceos