I am running Laravel 5 project and I created a controller in App->Mail->SendEmail to send emails with an attached system-generated invoice through mpdf, with the below code the invoice is generated but downloaded in my browser before being sent, and when changing the output to be s I get the error "Call to a member function output() on null"
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Order $order, $data, $mpdf)
{
$mpdf = new MpdfMpdf();
$mpdf->WriteHTML(view('invoices.paid_invoice', compact('order', 'data')));
$fileName = 'Invoice-'.$order->id.'.pdf';
$mpdf->Output($fileName,"D");
$this->order = $order;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.send_order')
->subject(('my subject'))
->attachData($this->mpdf->output(), 'invoice.pdf', [
'mime' => 'application/pdf',])
;
}}
2
Answers
In your code
$this->mpdf
references nothing.You dont need to use MDF anymore once you created the file so just save the filename and reuse that in the build function
As @N69S already mentioned in their answer, the mPDF object is not assigned to the class property. The best would be to avoid any "heavy lifting" in the constructor of the class. You need to call the Output method of mPDF exactly once. You can also create the mPDF object in the build method directly (or use a wrapper for Laravel providing a Facade, if that is your poison).
The main issue with provided examples is that with used second parameter
'D'
, the Output method echoes the PDF content directly (see $dest in Output method documentation) and does not return/save anything. You need either:To save the output to the
$this->filename
property (which can also be a local variable only in your example). This is done with$mpdf->Output($this->filename, 'F');
,Or output the binary data of the PDF as a string with
$mpdf->Output(null, 'S');
. If you don’t need to save the PDF on your server while generating the invoice, this is the cleanest option.Based on the variant chosen, the constructor will look like this:
and the build method will look like this:
or this: